0% found this document useful (0 votes)
45 views8 pages

Binary File Handling

The document contains code snippets demonstrating how to use Python's pickle module to write and read objects from binary files. It includes examples of writing employee, student, and other data objects to files, reading and searching the files to retrieve objects, and counting or summing values from the objects. Functions are defined to add records, count entries matching criteria, and retrieve data from the binary files.

Uploaded by

akshar.sharma94
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
45 views8 pages

Binary File Handling

The document contains code snippets demonstrating how to use Python's pickle module to write and read objects from binary files. It includes examples of writing employee, student, and other data objects to files, reading and searching the files to retrieve objects, and counting or summing values from the objects. Functions are defined to add records, count entries matching criteria, and retrieve data from the binary files.

Uploaded by

akshar.sharma94
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 8

'''import pickle

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":

rno=int(input("enter the roll no "))

name=(input("enter the name"))

marks=float(input("enter the marks"))

stu['Rollno']=rno

stu['Name']=name

stu['marks']=marks

pickle.dump(stu,s1)

ans=input("Do u want to enter more records")

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:

print("No such records")

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:

print("No such records")

else:

print("searched successfully")

fin.close()'''

#6.A binary file "sports.dat"has the fileds: S_code,S_name,Category,enrollments

'''a.write a user defined function newfile() to input data and add a record.

b.write a function s_count() in python which accepts the sports category

and counts the number of enrollment in the category.'''


'''def newfile():

import pickle

file=open("sports.dat","wb")

d={}

print("enter the sport details")

s_code=int(input("enter sno"))

s_name=input("enter sport name")

cat=input("enter the category")

enroll=int(input("enetr the no.of enrollments"))

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()

categ=input("enter the category")

s_count(categ)'''

#3.A binary file "Salesman.dat" has the fields : S_code,s_name,Area,sales amount

'''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

the total sales amount of that area'''

'''def addRec():

import pickle

file=open("salesman.dat","ab")

d={}

print("enter the salesman details")

s_code=int(input("enter sno"))

s_name=input("enter salesman name")

Area=input("enter the area")

s=float(input("enter the sales amount"))

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()

A=input("enter the Area")

sum_amount(A)

#4.A binary file "book.dat" has structure [BookNo,Book_Name,Book_Author,Price].

'''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()

Author=input("enter the Authorname")

countrec(Author)'''

#5.A binary file "Student.dat" has structure(admission_no,name,percenatge).write a function

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()'''

You might also like