-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbinary_operations.py
187 lines (164 loc) · 4.21 KB
/
binary_operations.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import pickle
def Append(l):
fwb=open("student1.dat","ab")
pickle.dump(l,fwb)
fwb.close()
print("data written succsessfully to binary file")
def read():
frb=open("student1.dat","rb")
l=pickle.load(frb)
print(l)
l=pickle.load(frb)
print(l)
l=pickle.load(frb)
print(l)
l=pickle.load(frb)
print(l)
frb.close()
def readall(file):
frb=open(file,"rb")
while True:
try:
l=pickle.load(frb)
print(l)
except EOFError:
break
frb.close()
def search_rno(r):
frb=open("student1.dat","rb")
found=0
while True:
try:
l=pickle.load(frb)
if r==l[0]:
print(l)
found=1
break #because rno is unique
except EOFError:
break
if found==0:
print("Roll no",r,"does not exist.")
frb.close()
def search_name(n):
#[1,'Aakarsh',99]
frb=open("student1.dat","rb")
found=0
while True:
try:
l=pickle.load(frb)
if n==l[1]: #made changes
print(l)
found=1
except EOFError:
break
if found==0:
print("Name",n,"does not exist.")
frb.close()
def update(r):
import os
frb=open("student1.dat","rb")
fwb=open("temp.dat","wb")
found=0
while True:
try:
l=pickle.load(frb)
if r==l[0]:
l[1]=input("Enter the new name")
l[2]=int(input("Enter the new marks"))
pickle.dump(l,fwb)
found=1
else:
pickle.dump(l,fwb)
except EOFError:
break
frb.close()
fwb.close()
os.remove("student1.dat")
os.rename("temp.dat","student1.dat")
if found==0:
print("Roll no",r,"does not exist.")
else:
print("Record updated....")
readall()
def delete(r):
import os
frb=open("student1.dat","rb")
fwb=open("temp.dat","wb")
found=0
while True:
try:
l=pickle.load(frb)
if r!=l[0]: #record not be deleted
pickle.dump(l,fwb)
else:
found=1
except EOFError:
break
frb.close()
fwb.close()
os.remove("student1.dat")
os.rename("temp.dat","student1.dat")
if found==0:
print("Roll no",r,"does not exist.")
else:
print("Record deleted....")
readall()
#to copy records matching the criteria from student1.dat to hundred.dat : copy paste
def copy():
frb=open("student1.dat","rb")
fwb=open("hundred.dat","wb")
count=0
while True:
try:
l=pickle.load(frb)
if l[2]==100: #record not be deleted
pickle.dump(l,fwb)
count+=1
except EOFError:
break
frb.close()
fwb.close()
print(count,"records copied to hundred.dat")
#to move records matching the criteria from student1.dat to hundred1.dat : cut paste
def transfer():
import os
frb=open("student1.dat","rb") #master file
fwb1=open("hundred1.dat","wb")#paste
fwb2=open("temp.dat","wb") #cut ..will be renamed to student1.dat later
count=0
while True:
try:
l=pickle.load(frb)
if l[2]==100: #record not be deleted
pickle.dump(l,fwb1)
count+=1
else:
pickle.dump(l,fwb2)
except EOFError:
break
frb.close()
fwb1.close()
fwb2.close()
os.remove("student1.dat")
os.rename("temp.dat","student1.dat")
print(count,"records moved to hundred1.dat")
#calling the UDFs
'''
Append([1,'Aakarsh',99])
Append([2,'Aakriti',98])
Append([3,'Abhinav',98])
'''
#Append([4,'Aditya',98])
#Append([5,'Akshat',98])
print("showing all records")
#read()
readall('student1.dat')
#print("search by name")
#search_name('Aakriti')
#search_rno(1)
#update(4)
#copy()
transfer()
readall('student1.dat')
readall('hundred1.dat')
#delete(4)