Binary File handling Assignment – Python (solved)


Binary File handling Assignment for Python is designed to give you an idea, how you can do different types of operation on a binary file in python using the pickle module. Python heavily depends on this module for binary file handling.

If you are new to binary file handling in Python then I would suggest you to either check my youtube video on binary file handling or read more on binary file handling in our blog.

Q1. Write a program in python to read a record of a student having admno, name, class and section from the keyboard and then save this data in a binary file ‘student.dat’.

import pickle

admno = input('Enter admission number :')
name = input('Enter student name :')
std = input('Enter class number :')
section= input('Enter section :')
student={'admno':admno,'name':name,'class':std,'section':section}

file = open('student.dat','wb')
pickle.dump(student,file)
file.close()

Q2. Write a program in python to read this newly created binary file ‘student.dat’ and display its contents on the screen

import pickle
file=open('student.dat','rb')
student = pickle.load(file)
print(student)
file.close()
BUT the above solution is not the perfect one as it is printing the result in the form of a dictionary.

Q3. Write a program in python to read multiple records of students having admno, name, class and section from the keyboard and then add these records in the already existing binary file ‘student.dat’.

import pickle
file = open('student.dat','ab')

while True:
  admno = input('Enter admission number :')
  name = input('Enter student name :')
  std = input('Enter class number :')
  section= input('Enter section :')
  student={'admno':admno,'name':name,'class':std,'section':section}
  pickle.dump(student,file)
  choice = input('Add more records (y/n').upper()
  if choice=='N':
    break

print('Record added student.dat file')
file.close()

Q4. Write a python program to read the contents of a binary file ‘student.dat’ created in the previous question and display its contents on the screen.

import pickle
file = open('student.dat','rb')
while True:
  try:
    student = pickle.load(file)
    print(student)
  except:
    break

file.close()

NOTE : Since binary file does not return a NULL value of a blank string from load() method thus , we use try catch block to catch runtime error to close this while loop.
Q5. Write a program in python to read an admission number from the keyboard. Delete all the records from binary file ‘student.dat’ having this entered admission no.

import pickle
import os

file1= open('student.dat','rb')
file2= open('temp.dat','wb')

admno = input('Enter admission no to delete :')

while True:
  try:
    student = pickle.load(file1)
    if student['admno']!=admno:  
        pickle.dump(student,file2)
  except:
    break
file1.close()
file2.close()

os.remove('student.dat')
os.rename('temp.dat','student.dat')

print('Record deleted ....')

Q6. Write a python program to read binary file ‘student.dat’ and then change the contents of admission number 102.

import pickle
import os

file1= open('student.dat','rb')
file2= open('temp.dat','wb')

admno = input('Enter admission no to delete :')
found=0
while True:
  try:
    student = pickle.load(file1)
    if student['admno']==admno:  
        student['name'] = input('Enter new name :')
        student['std'] = input('Enter new class :')
        student['section'] = input('Enter new section :')
        found=1
    pickle.dump(student,file2)
  except:
    break
file1.close()
file2.close()

os.remove('student.dat')
os.rename('temp.dat','student.dat')

print('Record updated ....') if found==1 else print('Admission no not found')

These are the core operation that every student must know in binary file.

Print Friendly, PDF & Email

Related Posts

If you like CBSEToaday and would like to contribute, you can also write an article using submit article or mail your article to contribute@cbsetoday.com See your article appearing on the cbsetoday.com main page and help other students/teachers.