Binary file programs
Binary file programs
1. WAP to a binary file called emp.dat and write into it, the employee details of
some employees, available in the form of dictionaries.
1 import pickle
2
3 e1 = {'No' : 1, 'Name' : 'Anushree', 'Age' : 25, 'Salary' : 47000}
4 e2 = {'No' : 2, 'Name' : 'Zoya', 'Age' : 30, 'Salary' : 48000}
5 e3 = {'No' : 3, 'Name' : 'Simarjeet', 'Age' : 27, 'Salary' : 49000}
6 e4 = {'No' : 4, 'Name' : 'Alex', 'Age' : 29, 'Salary' : 50000}
7
8 bin1 = open ("<file_name_with_extension>", "wb")
9
10 pickle.dump (e1, bin1)
11 pickle.dump (e2, bin1)
12 pickle.dump (e3, bin1)
13 pickle.dump (e4, bin1)
14
15 bin1.close()
2. WAP to get student data (roll no., name and marks) from user and write onto a
binary file. Program should be able to get data from user and write onto the file as
long as user wants.
1 import pickle
2
3 stu = {}
4
5 bin1 = open ("<file_name_with_extension>", "wb")
6
7 ans = 'y'
8
9 while ans == 'y' :
10 r = int (input ("Enter roll no. : "))
11 n = input ("Enter name : ")
12 m = int (input ("Enter marks : "))
13
14 stu['roll'] = r
15 stu['name'] = n
16 stu['marks'] = m
17
18 pickle.dump(stu, bin1)
19
20 ans = input ("Do you want to add more data ? (y / n) : ")
21
22 bin1.close()
3. WAP to open the file created in the previous program, read the objects written in
it and display them.
1 import pickle
2
3 d = {}
4
5 bin1 = open ("<file_name_with_extension>", "rb")
6
7 try :
8 while True :
9 d = pickle.load(bin1)
10 print (d)
11
12 except EOFError :
13 bin1.close ()
4. WAP to read file Stu.dat created earlier and display records having marks > 81.
1 import pickle
2 bin1 = open ("Stu.dat", "rb")
3 found = False
4
5 try :
6 print ("Searching in file . . . ")
7 while True :
8 stu = pickle.load (bin1)
9 if stu ['Marks'] > 81 :
10 print (stu)
11 found = True
12
13 except EOFError :
14 if found == False :
15 print ("No such records found in the file")
16 else :
17 print ("Search successful")
18
19 bin1.close ()
5. Consider the binary file Stu.dat created in the earlier programs. WAP to update
the file so that students with marks > 81 get another 2 marks.
1 import pickle
2 bin1 = open ("Stu.dat", "rb+")
3 found = False
4
5 try :
6 while True :
7 pos = bin1.tell ()
8 stu = pickle.load (bin1)
9
10 if stu ['Marks'] > 81 :
11 stu ['Marks'] += 2
12 bin1.seek (pos)
13 found = True
14
15 except EOFError :
16 if found == False :
17 print ("No matching records.")
18 else :
19 print ("Records successfully updated")
20
21 bin1.close ()
5. Consider a file, SPORT.DAT, containing records of the following structure:
[SportName, TeamName, No_Players]
Write a function, copyData(), that reads contents from the file SPORT.DAT and
copies the records with Sport name as “Basket Ball” to the file named BASKET.DAT.
The function should return the total number of records copied to the file
BASKET.DAT.
1 import pickle
2
3 def copyData () :
4 obj1 = open ("SPORT.dat", "rb")
5 obj2 = open ("BASKET.dat", "wb")
6 c = 0
7
8 try :
9 while True :
10 data = pickle.load (obj1)
11 print (data)
12
13 if data [0] == “Basket Ball” :
14 pickle.dump (data, obj2)
15 c += 1
16
17 except EOFError :
18 obj1.close ()
19 obj2.close ()
20
21 return c