0% found this document useful (0 votes)
13 views

Binary File

Uploaded by

Rhythm Mazumdar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Binary File

Uploaded by

Rhythm Mazumdar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Binary File

• Binary Files: These are files that store data in binary format. They are
used for storing non-text data such as images, audio, video, and other
multimedia. The advantage of binary files is that they are faster and
more efficient for storing complex data structures.
• Pickle Module: The pickle module in Python is used for
serializing and deserializing Python objects. Serialization
is the process of converting a Python object into a byte
stream, and deserialization is the process of converting
a byte stream back into a Python object.
Operations on Binary Files
• file = open("file_name", "mode")

# file_name = Name of the file


# mode: Mode in which the file is to be opened, e.g., 'rb' for reading, ‘
wb' for writing, 'ab' for appending.
What is pickle
• pickle refers to the process of converting the structure such as list or
dictionary to a byte stream before writing to the file.

pickle.dump (dump is used for write and pickle is module)


pickle.dump(<structure>,fileobject)

pickle.load (load is used for read)


structure = pickle.load(fileobject)
Writing to binary file

Writing sting in binary


• import pickle
• name = "string” # Predefined string
• file = open("string.dat","wb") #using ‘wb’ means write in binary
• pickle.dump(name,file) # here dump is for writing, name is
called structure.
Reading Binary File in string
• file = open("string.dat","rb") # rb mode for read in binary
• data = pickle.load(file) # load is used for read the binary
• print(data)

Output
Write the list in a binary file and
Read the list
List
Using nested list, store multiple
employee records in a binary file
demo1.dat
Reads the employee records from the binary file demo1.dat and prints them
on the screen

output
Search the record of particular
employee

Enter 1 for checking employee details

Details of employee 1
Update the employee salary

Enter 1 for update


salary

Enter the updated salary


New updated salary
Using list enter employee details
Read the binary file

We are using try and except for handling


the EOF error
Read the binary file
Search the employee data
Update the employee salary

You might also like