How to Write data into a Binary File in Python | Writing data into a binary file using Python
To write an object to a binary file opened in the write mode, we should use dump( ) function of pickle module as per the following syntax :
pickle.dump(<object>, <file-handler>)
For example, if you have a file open in handle f1 as
f1=open(“file1.txt”,”wb”)
1. Python program to write a list namely list1 in the file named file1.txt
import pickle f1=open("file1.txt","wb") list1=[1,'Lovejot','Teacher'] pickle.dump(list1,f1) f1.close()
2. Python program to write a tuple namely t1 in the file named file2.txt
import pickle f2=open("file2.txt","wb") t1=(2,'Sumit','XII'] pickle.dump(t1,f2) f2.close()
3. Python program to write a dictionary namely d1 in the file named file3.txt
import pickle f3=open("file3.txt","wb") d1={'Rollno':1, name:'Anil'} pickle.dump(d1,f3) f3.close()
4. Python program to create a binary file named employee.dat and write details of employees available in the form of dictionaries.
import pickle # dictionary objects E1={ ‘Empno’ :101, ‘Name’ : ‘Ramesh’ , ‘Salary’ : 17000} E2={ ‘Empno’ :102, ‘Name’ : ’’ , ‘Age’ : 30, ‘Salary’ : 18000} #open file in write mode file1 = open (‘Employee.dat’ , ‘wb’) #write to the file pickle.dump (E1 ,file1) pickle.dump (E2 , file1) #close file file1.close( )
5. Python program to create a binary file named student.dat . Data of students must be put at run time then it should be stored in file. The structure of data is (rollno, name, marks).
import pickle s={ } #declare empty dictionary file2 = open(‘student.dat’ , ‘wb’) #open file R = int (input (“Enter roll number=“ ) ) N = input (“Enter name =“ ) M = float(input (“Enter marks =“ ) # add read data into dictionary S [‘Rollno’] = R S [‘Name’] = N S[‘Marks’] = M # now write into the file pickle.dump (s ,file2) file2.close( )
Appending Records in Binary File in Python
Appending records in binary files is similar to writing record but there are two differences:
i. We need to open binary file in append mode (“ab”).
ii. If file doesn’t exit, it will create a new file .
iii. If file already exits, it will write new record at the end of existing file.
# Python program to append a list namely list1 in the file named file1.txt
import pickle f1=open("file1.txt","ab") list1=[2,'Ayush','Student'] pickle.dump(list1,f1) f1.close()
#Python program to append a tuple namely t1 in the file named file2.txt
import pickle f2=open("file2.txt","ab") t1=(3,'Sunita','XI'] pickle.dump(t1,f2) f2.close()
#Python program to append a dictionary namely d1 in the file named file3.txt
import pickle f3=open("file3.txt","ab") d1={'Rollno':2, name:'Sumit'} pickle.dump(d1,f3) f3.close()
#Python program to append student records to an existing file student.dat by getting input from user.
import pickle Students ={ } #empty dictionary created to store records file1 = open ('Student.dat', 'ab') choice = 'y' while choice == 'y' : #Read values of rollno, name and marks R=int(input("Enter roll number of student = ")) N=input("Enter name =") M=float (input("Enter marks = ")) #put values of variables R,N, M in dictionary Students['Rollno'] =R Students['Name'] =N Students['Marks'] =M pickle.dump (Students, file1) #Write record into the file choice=input("Do you want to add more records (y/n)) file1.close( )
Output
Enter roll number of student = 101 Enter name =anita Enter marks = 88 Do you want to add more records (y/n)…y Enter roll number of student = 102 Enter name =sunil Enter marks = 56 Do you want to add more records (y/n)n
Click here for Quiz on File Handling in Python
Popular Books of Computer Science