0% found this document useful (0 votes)
80 views3 pages

Binary Files CRUD Operations - Jupyter Notebook

The document defines functions for performing CRUD (create, read, update, delete) operations on a student data file. It includes functions to add, display, search, delete, and edit student records stored as pickled data. The main function runs a menu loop calling the appropriate function based on the user's input choice to manage the student records.

Uploaded by

lanfury45
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
80 views3 pages

Binary Files CRUD Operations - Jupyter Notebook

The document defines functions for performing CRUD (create, read, update, delete) operations on a student data file. It includes functions to add, display, search, delete, and edit student records stored as pickled data. The main function runs a menu loop calling the appropriate function based on the user's input choice to manage the student records.

Uploaded by

lanfury45
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

In [*]:  1 def add_student(stu): #stu will be a list -> rno, name, marks

2 import pickle as p
3 f = open('student.dat', 'ab')
4 p.dump(stu, f)
5 f.close()
6 ​
7 def display():
8 import pickle as p
9 f = open('student.dat', 'rb')
10 while True:
11 try:
12 stu = p.load(f)
13 print(stu)
14 except EOFError:
15 print("End of File")
16 break
17 f.close()
18
19 def search(name):
20 import pickle as p
21 f = open('student.dat', 'rb')
22 flag = 0
23 while True:
24 try:
25 stu = p.load(f)
26 if name.lower() in stu[1].lower():
27 flag=1
28 print("Rno :", stu[0])
29 print("Name :", stu[1])
30 print("Marks :", stu[2])
31 print("-------------------------\n")
32
33 except EOFError:
34 print("Search Completed")
35 break
36 if flag == 0:
37 print("No match found!\n")
38 f.close()
39
40 def delete(rno):
41 #delete -> copy - make change - delete original
42 import pickle as p
43 f = open('student.dat', 'rb')
44 flag = 0
45 TEMP = []
46 while True:
47 try:
48 stu = p.load(f)
49 if stu[0] != rno:
50 TEMP.append(stu)
51 else:
52 print("Record of ",stu[1], "deleted from file.")
53 flag=1
54 except EOFError:
55 print("EOF")
56 break
57 f.close()
58 if flag == 0:
59 print("Roll No not found!\n")
60 else:
61 f = open('student.dat', 'wb')
62 for stu in TEMP:
63 p.dump(stu,f)
64 f.close()
65
66 def edit(rno, new_marks):
67 #delete -> copy - make change - delete original
68 import pickle as p
69 f = open('student.dat', 'rb')
70 flag = 0
71 TEMP = []
72 while True:
73 try:
74 stu = p.load(f)
75 if stu[0] != rno:
76 TEMP.append(stu)
77 else:
78 stu[2] = new_marks
79 TEMP.append(stu)
80 print("Record of ",stu[1], "updated in file.")
81 flag=1
82 except EOFError:
83 print("EOF")
84 break
85 f.close()
86 if flag == 0:
87 print("Roll No not found!\n")
88 else:
89 f = open('student.dat', 'wb')
90 for stu in TEMP:
91 p.dump(stu,f)
92 f.close()
93
94 ​
95 #CRUD OPERATIONS
96 def main():
97 menu = '''\n----------------Menu---------------
98 1. Add Student
99 2. Edit Marks
100 3. Delete Student
101 4. Search
102 5. Display All
103 6. Exit
104 '''
105 while True:
106 print(menu)
107 ch = input("Enter your choice (1-6) : ")
108 if ch == '1':
109 rno = int(input("Enter Rno: "))
110 name = input("Enter Name: ")
111 marks = float(input("Enter Marks:"))
112 S = [rno, name, marks]
113 add_student(S)
114 print("Inserted Successfully!")
115 elif ch == '2':
116 rno = int(input("Enter rno of the record to be deleted : "))
117 new_marks = float(input("Enter updated marks : "))
118 edit(rno, new_marks)
119 elif ch == '3':
120 rno = int(input("Enter rno of the record to be deleted : "))
121 delete(rno)
122 elif ch == '4':
123 name = input("Enter name to search for : ")
124 search(name)
125 elif ch == '5':
126 display()
127 elif ch == '6':
128 break
129 else:
130 print("Invalid input! Try again")
131 ​
132
133
134 main()

----------------Menu---------------
1. Add Student
2. Edit Marks
3. Delete Student
4. Search
5. Display All
6. Exit

Enter your choice (1-6) : 2


Enter rno of the record to be deleted : 2
Enter updated marks : 15
EOF
Roll No not found!

----------------Menu---------------
1. Add Student
2. Edit Marks
3. Delete Student
4. Search
5. Display All
6. Exit

Enter your choice (1-6) : 2


Enter rno of the record to be deleted : 3
Enter updated marks : 15
Record of Atul updated in file.
EOF

----------------Menu---------------
1. Add Student
2. Edit Marks
3. Delete Student
4. Search
5. Display All
6. Exit

Enter your choice (1-6) : 5


[1, 'Abhishek', 11.0]
[3, 'Atul', 15.0]
End of File

----------------Menu---------------
1. Add Student
2. Edit Marks
3. Delete Student
4. Search
5. Display All
6. Exit

Enter your choice (1-6) :

In [ ]:  1 ​

You might also like