Binary File Handling
Binary File Handling
emp1={'empno':1201,'Name':"AAA","Age":25,"salary":47000}
emp2={'empno':1202,'Name':"bbb","Age":26,"salary":50000}
#emp3={'empno':1203,'Name':"ccc","Age":27,"salary":52000}
#emp4={'empno':1204,'Name':"ddd","Age":28,"salary":54000}
e1=open('emp1.dat',"wb")
pickle.dump(emp1,e1)
pickle.dump(emp2,e1)
#pickle.dump(emp3,e1)
#pickle.dump(emp4,e1)
print("Success")
e1.close()'''
#2.WAP to get stud data from user and write onto a binary file.
'''import pickle
stu={}
s1=open("stu.dat","wb")
ans="y"
while ans=="y":
stu['Rollno']=rno
stu['Name']=name
stu['marks']=marks
pickle.dump(stu,s1)
s1.close()'''
#3.WAP to open the stu.dat read the objects written in it and display them.
'''import pickle
student={}
stufile=open('stu.dat','rb')
try:
while True:
student=pickle.load(stufile)
print(student)
except EOFError:
stufile.close()'''
#4.Searching a file
'''import pickle
stu={}
found=False
fin=open('stu.dat','rb')
search=[4]
try:
print("searching in file")
while True:
stu=pickle.load(fin)
if stu['Rollno'] in search:
print(stu)
found=True
print(found)
except EOFError:
if found== False:
else:
print("searched successfully")
fin.close()
'''
#5.Searching a file
'''import pickle
stu={}
found=False
fin=open('stu.dat','rb')
try:
print("searching in file")
while True:
stu=pickle.load(fin)
if stu['marks']>=90:
print(stu)
found=True
except EOFError:
if found== False:
else:
print("searched successfully")
fin.close()'''
'''a.write a user defined function newfile() to input data and add a record.
import pickle
file=open("sports.dat","wb")
d={}
s_code=int(input("enter sno"))
d['s_code']=s_code
d['s_name']=s_name
d['cate']=cat
d['enroll']=enroll
pickle.dump(d,file)
file.close()
newfile()'''
'''def s_count(c):
import pickle
count=0
file=open("sports.dat","rb")
try:
while True:
d=pickle.load(file)
if d['cate']==c:
x=d['enroll']
count+=x
print(count)
except EOFError:
file.close()
s_count(categ)'''
'''a) Write user defined function addRec() to input data and add a record of a salesman
b)Write function sum_amt() in python which accepts the area from the user and finds
'''def addRec():
import pickle
file=open("salesman.dat","ab")
d={}
s_code=int(input("enter sno"))
d['s_code']=s_code
d['s_name']=s_name
d['Area']=Area
d['Sales_Amount']=s
pickle.dump(d,file)
file.close()
addRec()'''
'''def sum_amount(c):
import pickle
file=open("salesman.dat","rb")
try:
while True:
d=pickle.load(file)
if d['Area']==c:
x=d['Sales_Amount']
print(x)
except EOFError:
file.close()
sum_amount(A)
'''a.Write a user defined function createfile() to input data for a record and add to book.dat
b.Write a function countrec(author) in python which accepts the Author name as parameter and
count and return number of books by the author are stored in the binary file"book.dat" '''
'''
rec=[BookNo,Book_Name,Book_Author,Price]
pickle.dump(rec,file)
file.close()
createfile()
def countrec(Author):
import pickle
file=open("Book.dat","rb")
cnt=0
try:
while True:
d=pickle.load(file)
if Author==rec[2]:
cnt=cnt+1
print(cnt)
except EOFError:
file.close()
countrec(Author)'''
countrec() in python that would read the contents of the file "stu.dat" and display the details
of those students whose % is above 75.Also display number of student scoring above 75%.
'''def countrec():
import pickle
file=open("stu.dat","rb")
num=0
try:
while True:
d=pickle.load(file)
if d[2]>75:
print(d[0],d[1],d[2],sep="\t")
num=num+1
except EOFError:
file.close()
countrec()'''