-
Notifications
You must be signed in to change notification settings - Fork 0
/
File handling in menu
154 lines (126 loc) · 3.69 KB
/
File handling in menu
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import pickle
def Createfile():
# to create a link with a file
file = open("student.dat","wb")
#getting inputs
rno = int(input("Enter roll no of student:"))
Name = input("Enter name of the Student:")
marks= eval(input("Enter marks in comp. sci. subject (out of 40):"))
#insetring object into structure data
stu= [rno, Name,marks]
# dumping / pickling stu object to a file.
pickle.dump(stu,file)
print("File created ....")
# closing a link with a file.
file.close()
def Appendfile():
# to create a link with a file
file = open("student.dat","ab")
#getting inputs
rno = int(input("Enter roll no of student:"))
Name = input("Enter name of the Student:")
marks= eval(input("Enter marks in comp. sci. subject (out of 40):"))
#insetring object into structure data
stu= [rno, Name,marks]
# dumping / pickling stu object to a file.
pickle.dump(stu,file)
print("File created ....")
# closing a link with a file.
file.close()
def Readfile():
file = open("student.dat","rb")
try:
while True:
stu= pickle.load(file)
print(stu)
except EOFError:
pass
file.close()
def Searchfile():
file = open("student.dat","rb")
rol=int(input("Enter roll no. to be search in the file:"))
flag=0
try:
while True:
stu= pickle.load(file)
if stu[0]==rol:
print("Roll No.:",stu[0])
print("Name:",stu[1])
print("Marks In CS:",stu[2])
flag=1
break
except EOFError:
pass
if flag == 0:
print("Record not found in the file.")
file.close()
def Deleterec1():
rno = int(input("Enter a roll no you want to delete:"))
rfile = open("student.dat","rb")
reclst= []
flag =0
try:
while True:
rec = pickle.load(rfile)
reclst.append(rec)
except EOFError:
pass
rfile.close()
#print(reclst)
wfile= open("student.dat","wb")
for rec in reclst:
if rec[0]== rno:
flag =1
print("record found and deleted..")
continue
else:
pickle.dump(rec,wfile)
wfile.close()
if flag == 0:
print("Record not found in the file.")
def Deleterec():
import os
rno = int(input("Enter a roll no you want to delete:"))
rfile = open("student.dat","rb")
wfile = open("temp.dat","wb")
flag =0
try:
while True:
rec = pickle.load(rfile)
if rec[0]== rno:
flag =1
print("record found & deleted.")
continue
else:
pickle.dump(rec,wfile)
except EOFError:
pass
rfile.close()
wfile.close()
os.remove("student.dat")
os.rename("temp.dat","student.dat")
if flag ==0:
print("Record not found....")
'''### ..................... ######'''
ans = 'y'
while ans =='y':
print("Program menu to work with a binary file")
print("1. TO create a File.")
print("2. To append a file.")
print("3. To read from a file.")
print("4. To search in binary file.")
print("5. To Delete a record from a file.")
choice = int(input("Enter your choice:"))
if choice == 1:
Createfile()
elif choice == 2:
Appendfile()
elif choice == 3:
Readfile()
elif choice == 4:
Searchfile()
elif choice == 5:
Deleterec()
else:
print("Invalid choice ...")
ans = input("Do you want to perfrom operations? Enter y for YES):")