Display all the records from binary file
1. open the binary file in read mode
2. if file does not exist then an error will be displayed
3. using a while loop read a binary file
4. read a record from binary file using pickle.load()
5. display the record
6. When EOF is reached close the file.
import pickle #function definition def std_dispall(): try: f=open("stud","rb") while True: rec=pickle.load(f) print(rec) except EOFError: f.close() except IOError: print("Unable to open the file") #function calling std_dispall()
Display all the records from binary file (2nd Method)
1. open the binary file in read mode
2. if file does not exist then an error will be displayed
3. using a while loop read a binary file
4. read a record from binary file using pickle.load()
5. display the record
6. When EOF is reached close the file.
import pickle import os #function definition def std_dispall_1(): os.system("cls") try: f=open("stud","rb") print(" "*20,"*"*20) print(" "*23,"Student Details") print(" "*20,"*"*20) print("="*60) print("%-20s"%"Roll","%-20s"%"Name","%-20s"%"Per") print("="*60) while True: rec=pickle.load(f) print("%-20s"%rec[0],"%-20s"%rec[1],"%-20s"%rec[2]) except EOFError: f.close() print("="*60) except IOError: print("Unable to open the file") #function calling std_dispall_1()