Preview
How to Read data from a Binary File in Python | Reading data from binary file in Python
After writing data into binary file using dump() method of pickle module, we can read and display content of binary file load( ) method of the pickle module. Reading data from binary file is known as unpickling.
The syntax of load( ) method is
<object> =pickle.load(<filehandle>)
#Program to read and display data stored in binary file Employee.dat as created in program 4 given in previous post.
import pickle Emp = {} #Empty dictionary object to stored records read from Employee.data Empfile = open('Employee.dat', 'rb') #open binary file in read mode try: while True : # It will become False upon EOF Emp = pickle.load(F) #Read record in Emp dictionary from file handle Empfile print (Emp) # print the record except EOFError : Empfile.close()
Output
{ ‘Empno’ :101, ‘Name’ : ‘Ramesh’, ‘Age’ : 40, ‘Salary’ : 17000} { ‘Empno’ :102, ‘Name’ : ’Mohan’ , ‘Age’ : 30, ‘Salary’ : 18000}
#Program to create a binary file named myfile.dat and store a string value in it.
import pickle S =”This is string value” with open(“myfile.dat”, “wb”) as f : pickle.dump (S,f) print(“file successfully created.”) f.close()
Above program created a binary file namely myfile.dat that stored the string variable S in binary format.
# Program to read data from the file myfile.dat created in previous program and display all the text before the letter ‘v’,
import pickle S = ‘‘ with open (“myfile.dat”, “rb”)as f : S = pickle.load(fh) S1 =S.split(‘v’) print(S1[0]) f.close()
Output
This is string
Searching record from a binary file in Python
To search something in a file, we need to follow given steps:
- Open the file in read mode.
- Read the contents of file record by record
- Look for the desired item in record.
- If found, “Value is found”.
- If not found, read the next record and look for the desired item.
- If desired item is not found in any of the records, “No such item found“.
# Program to open file Employee.dat containing following records. Search for records with Empno=101. If found, display the record.
{‘Empno’ :101, ‘Name’ : ‘Ramesh’, ‘Age’ : 40, ‘Salary’ : 17000} {‘Empno’ :102, ‘Name’ : ’Mohan’ , ‘Age’ : 30, ‘Salary’ : 18000}
import pickle emp ={ } # empty dictionary object to hold records found = False F = open(‘Employee.dat’, ‘rb’) # open binary file in read mode try : print (“Searching in file Employee.dat… “) while True: # It will become False upon EOF(End of file) exception emp=pickle.load(F) #read record in emp dictionary from file handle F if emp[‘Empno’]==101: print (emp) #print the record found = True except EOFError : if found == False : print (“No such records found in the file “) else : print(“Search successful.”) F.close( ) #close file
Output
Searching in file stu.dat… {‘Empno’ :101, ‘Name’ : ‘Ramesh’, ‘Age’ : 40, ‘Salary’ : 17000} Search successful.
# Program to open file Employee.dat containing following records. Search for records with age>30. If found, display the record.
{‘Empno’ :101, ‘Name’ : ‘Ramesh’, ‘Age’ : 40, ‘Salary’ : 17000} {‘Empno’ :102, ‘Name’ : ’Mohan’ , ‘Age’ : 30, ‘Salary’ : 18000} import pickle emp ={} # empty dictionary object to hold records found = False F = open(‘Employee.dat’, ‘rb’) # open binary file in read mode try : print (“Searching in file Employee.dat… “) while True: # It will become False upon EOF(End of file) exception emp=pickle.load(F) #read record in emp dictionary from file handle F if emp[‘Age’]>30: print (emp) #print the record found = True except EOFError : if found == False : print (“No such records found in the file “) else : print(“Search successful.”) F.close( ) #close file
Output
Searching in file stu.dat… {‘Empno’ :101, ‘Name’ : ‘Ramesh’, ‘Age’ : 40, ‘Salary’ : 17000} Search successful.
Click here for Quiz on Python Basics
Popular Books of Computer Science