BINARY FILE HANDLING ASSIGNMENT(2024-25)
BINARY FILE HANDLING ASSIGNMENT(2024-25)
1. _______________ will return the current position of file pointer in the file
a. seek() b) search() c) tell() d) print()
2. ________ places the file pointer at the specified position in the open file.
a. seek() b) search() c) tell() d) print()
3. F.seek(20,0) will move the file pointer 20 bytes in forward direction from beginning of
file. State True or False
4. F1.seek(-5,1) will move the file pointer 5 bytes backwards from end of file. State True
or False
5. Syntax of seek function in Python is myfile.seek(offset, reference_point) where myfile
is the file object. What is the default value of reference_point?
a. 0 b. 1 c. 2 d. 3
6. A binary file “Book.dat” has structure
[BookNo, Book_Name, Author, Price]
Write the Python program to input data for a record and add to Book.dat.
7. A binary file “Employee.dat” has structure
[empl_no, emp_name, age, salary]
Write the Python program to print the records of those employee whose salary is greater t
than 20000.
8. Consider a Binary file ‘Book.dat’ which stores data of books, where each record of the
book is as
[Bookid, Bookname, Price]
a. 1300
b. 2565
c. 3865
d. None of the above
12. Consider a file FLIGHT.DAT containing multiple records. The structure of each record is as shown
below: [Fno, FName, Fare, Source, Destination] Write a function COPY_REC() in Python that copies all
those records from FLIGHT.DAT where the source is DELHI and the destination is MUMBAI, into a
new file RECORD.DAT
13. Consider a Binary file BOOK.DAT containing a dictionary having multiple elements. Each element is in
the form BNO:[BNAME,BTYPE,PRICE] as key:value pair
where
BNO – Book Number
BNAME – Book Name
BTYPE - Book Type
PRICE – Book pric
Write a user-defined function, findBook(price), that accepts price as parameter and displays all those
records from the binary file BOOK.DAT which has a book price more than or equal to the price value
passed as a parameter
19. A Binary file Stock.dat has a structure [pno,pname,qty,price].A user defined function
Createfile() to input data for 3 records and add to stock.dat .There are some blanks
help in filling the gaps in the code:
Incomplete Code :
Import ___________ # Statement 1
def createfile():
File=open(“d:\\Stock.dat”,‟____‟) #Statement 2
pno=input(“Enter product no:”)
pname= input(“Enter product name:”)
qty= input(“Enter product quantity:”)
price= input(“Enter product price:”)
record=[pno,pname,qty,price
_______________ # Statement 3
Print(“Record inserted”)
File.close()
Createfile()
20. A binary file “STUDENT.DAT” has structure [admission_number, Name, Percentage]. Write a function
countrec() in Python that would read contents of the file “STUDENT.DAT” and display the details of
those students whose percentage is above 75. Also display number of students scoring above 75%.
21. A binary file “salary.DAT” has structure [teacherid, teacher name, salary].
Complete the code in the blanks so that it would read contents of the file
“salary.DAT” and display the details of those teachers whose salary is above
20000.
import pickle
____________________________ # line1
try:
print("tr id\t tr Name\t tr Sal")
while True:
rec=___________.load(fobj) #line2
if rec[2]>_______: #line3
print(rec[0],"\t\t",rec[1],"\t\t",rec[2])
except:
____.close() #line 4
22. Mr.Rohan wants to modify salary of employee having a structure[eid,ename ,salary],but unable to fill
the gaps in the code. Help him to complete the code
import pickle
f = open('d:/student.dat','rb')
reclst = []
r=___________________________ # line 1 code to ask employee id
m=int(input(“enter correct salary”))
while True:
try:
rec = pickle.load(f)
reclst.append(rec) #line2 statement to add items in list at the end one by one
except EOFError:
break
f.close()
for i in range (len(reclst)):
if reclst[i]['eid']==r:
reclst[i]['salary'] = m
f = open('d:/student.dat','___') #line 3 mode to be used to copy the data
for x in reclst:
pickle.dump(x,f)
f.close()
23. A binary file sports.dat contains information in the following structure:( Event, Participant )
A code is shown below which is incomplete that would read contents from the sports.dat and creates
a file named Athletic.dat copying only those records from
sports.dat where the event name is “Athletics”.
import pickle
ath ( f1 , f2 ) :
l = pickle.load ( f1)
for t in l :
if ( t [ 0 ] == “________________” ) : #line 1
pickle.__________ ( t , f2 ) #line 2
f1 = open ( “ sports.dat “ , “ rb ” )
f2 = open ( “ athletics.dat “ , “ wb “ )
f.close()
f1.close()
24. A function searchprod( pc) in python is created to display the record of a particular
product from a file product.dat whose code is passed as an argument. Structure of
product contains the following elements [product code , product price].There is
some problem in completing the code,help to finish the code:
f = ________('d:/product.dat','rb') #line1
flag = False
pc=input(“Enter product code to be searched”)
while True:
try:
rec = pickle.load(f)
if rec['pcode'] ==_____: #line2
print('Product code:',rec['pcode'])
print('Price:',rec['price'])
flag = True
except EOFError:
break
if flag == False:
print('No Records found')
f.close()