-
Notifications
You must be signed in to change notification settings - Fork 1
/
listfilecompilation.py
132 lines (124 loc) · 3.26 KB
/
listfilecompilation.py
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
import pickle
import os
#1-write
def fwrite():
ans='y'
fout=open("list1.dat",'wb')
while ans=='y':
rno=int(input("Enter the Roll No."))
name = input("Enter a name")
marks = float(input("Enter the Marks"))
data=[rno,name,marks]
pickle.dump(data,fout)
ans=input("Do you want to continue - y or n")
print("Data Written")
fout.close()
#2-read
def fread():
fin=open("list1.dat",'rb')
try:
while True:
st_rec=pickle.load(fin)
print(st_rec)
except EOFError:
fin.close()
#3-append
def fappend():
ans='y'
fout=open("list1.dat",'ab')
while ans=='y':
rno=int(input("Enter the Roll No."))
name = input("Enter a name")
marks = float(input("Enter the Marks"))
data=[rno,name,marks]
pickle.dump(data,fout)
ans=input("Do you want to continue - y or n")
print("Data Written")
fout.close()
#4-modify
def fmodify():
fin=open("list1.dat",'rb')
fout=open("temp2.dat",'ab')
rno=int(input("Enter roll no. to be modified"))
found=False
try:
while True:
st_rec=pickle.load(fin)
if st_rec[0]==rno:
print(st_rec)
st_rec[0]=int(input("Enter the Roll No."))
st_rec[1]=input("Enter name to be modified")
st_rec[2]=float(input("Enter marks to be modified"))
print(st_rec)
found=True
pickle.dump(st_rec,fout)
else:
pickle.dump(st_rec,fout)
except EOFError:
if found==False:
print("Record not Found")
else:
print("Record updated")
fin.close()
fout.close()
os.remove("list1.dat")
os.rename("temp2.dat","list1.dat")
#5-delete
def fdelete():
fin=open("list1.dat",'rb')
fout=open("temp2.dat",'ab')
rno=int(input("Enter roll no. to be deleted "))
found=False
try:
while True:
st_rec=pickle.load(fin)
if st_rec[0]!=rno:
print(st_rec)
found=True
pickle.dump(st_rec,fout)
else:
print('rec deleted')
except EOFError:
if found==False:
print("Record not Found")
else:
print("Record updated")
fin.close()
fout.close()
os.remove("list1.dat")
os.rename("temp2.dat","list1.dat")
#6-fsearch
def fsearch():
fin=open("list1.dat",'rb')
rno=int(input("Enter roll no. to be searched "))
found=False
try:
while True:
st_rec=pickle.load(fin)
if st_rec[0]==rno:
print(st_rec)
found=True
except EOFError:
if found==False:
print("Record not Found")
else:
print("Record found")
y='y'
while y=='y':
a=int(input("ENTER YOUR CHOICE\n1-write\n2-read\n3-append\n4-modify\n5-delete\n6-search\n"))
#1-write\n2-read\n3-append\n4-modify\n5-delete\n
if a==1:
fwrite()
elif a==2:
fread()
elif a==3:
fappend()
elif a==4:
fmodify()
elif a==5:
fdelete()
elif a==6:
fsearch()
else:
print("pls enter a valid input")
y=input("do you want to continue (y/n)")