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

Binary Files - File Handling

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Binary Files - File Handling

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

File Handling

Working with Binary Files


• Binary files are used to store binary data such as images,
video files, audio files etc. They store data in the binary
format (0’s and 1’s) .

• In Binary files there is no delimiter for a line.

• The open() function open the file in text format by default.

• Pickle module can be imported to write or read data in a


binary file.
File Handling
Working with Binary Files

• To open files in binary mode, when


specifying a mode, add 'b' to it.

• Hence the “rb” file mode opens the file


in binary format for reading

• “wb” file mode open the file in binary


format for writing.
Binary Files
Binary files are also stored in terms of bytes (0s and 1s), but unlike text files, these
bytes do not represent the ASCII values of characters. Rather, they represent the
actual content such as image, audio, video, compressed versions of other files,
executable files, etc. These files are not human readable. Thus, trying to open a
binary file using a text editor will show some garbage values. We need specific
software to read or write the contents of a binary file. Binary files are stored in a
computer in a sequence of bytes. Even a single bit change can corrupt the file and
make it unreadable to the supporting application. Also, it is difficult to remove any
error which may occur in the binary file as the stored contents are not human
readable. We can read and write both text and binary files through Python
programs.
File Handling
Working with Binary Files

• Unlike text files, binary files are not human readable.

• When opened using a text editor, the data is


unrecognizable.
THE PICKLE MODULE
We know that Python considers everything as an object. So, all data types including list,
tuple, dictionary, etc. are also considered as objects. During execution of a program, we may
require to store current state of variables so that we can retrieve them later to its present
state. Suppose you are playing a video game, and after some time, you want to close it. So,
the program should be able to store the current state of the game, including current
level/stage, your score, etc. as a Python object. Likewise, you may like to store a Python
dictionary as an object, to be able to retrieve later.

To save any object structure along with data, Python provides a module called Pickle. The
module Pickle is used for serializing and de-serializing any Python object structure.
Pickling is a method of preserving food items by placing them in some solution, which
increases the shelf life. In other words, it is a method to store food items for later
consumption. Serialization is the process of transforming data or an object in memory (RAM)
to a stream of bytes called byte streams.
These byte streams in a binary file can then be stored in a disk or in a database or sent
through a network. Serialization process is also called pickling.
De-serialization or unpickling is the inverse of pickling process where a byte stream is
converted back to Python object. The pickle module deals with binary files. Here, data are
not written but dumped and similarly, data are not read but loaded.

The Pickle Module must be imported to load and dump data. The pickle module provides
two methods - dump() and load() to work with binary files for pickling and unpickling,
respectively.
PICKELINGAND UNPICKLING USING
PICKEL MODULE

• Pickle module can be imported to


write or read data in a binary file.
• First we need to import the module, It
provides two main methods for the
purpose:-
1) dump() method
2) load() method
File Handling
Binary file Operation
using pickle module
pickle.dump() function is used to store the object data to the file. It
takes 3 arguments.First argument is the object that we want to
store. The second argument is the file object we get by opening the
desired file in write-binary (wb) mode. And the third argument is
the key-value argument. This argument defines the protocol. There
are two type of protocol – pickle.HIGHEST_PROTOCOL and
pickle.DEFAULT_PROTOCOL.

pickle.load() function is used to retrieve pickled data.The steps are


quite simple. We have to use pickle.load() function to do that. The
primary argument of pickle load function is the file object that you
get by opening the file in read-binary (rb) mode.
PICKELINGAND UNPICKLING USING
PICKEL MODULE

Use pickle.dump() method to write the


object in file which is opened in binary access
mode.

Syntax of dump method is:

dump(object,fileobject)
pickle.dump() Method
Example: A program to write list sequence in a binary file
pickle.dump() Method

OUTPUT

Once you try to open python editor to see the


content python generates decoding error
pickle.load() Method
pickle.load() method is used to read the
binary file.
pickle.load() Method

OUTPUT
File Handling
Binary file R/W Operation
import pickle
using pickle module
output_file = open("a.bin", "wb") In this program we open a.bin file in
myint = 42
mystring = "Python!" binary mode using ‘wb’ specification
mylist = ["python", "sql", "mysql"] and create and store value in 4
mydict = { "name": "ABC", "job": "XYZ" }
pickle.dump(myint, output_file) different data objects i.e.
pickle.dump(mystring, output_file) int,string,list,dict.Write these into
pickle.dump(mylist, output_file)
pickle.dump(mydict, output_file) binary file using pickle.dump()
output_file.close() method then close this file and open
input_file = open("a.bin", "rb")
myint = pickle.load(input_file) the same for reading operation.We
mystring = pickle.load(input_file) read the content using load method()
mylist = pickle.load(input_file)
mydict = pickle.load(input_file) and display the value read from file.
print("myint = %s" % myint) To use dump and load we have to
print("mystring = %s" % mystring)
print("mylist = %s" % mylist) import pickle module first of all.
print("mydict = %s" % mydict)
input_file.close()
File Handling
Writing into Binary Files
Example: Write dictionary data to a Binary File:

import pickle
e={'Namita':25000,'Manya':30000,'Tanu':20000}
f1=open('emp.dat','wb')
pickle.dump(e,f1)
f1.close()
File Handling
Iteration over
import pickle Binary file - pickle module
output_file = open("a.bin", "wb")
myint = 42
mystring = "Python.mykvs.in!"
mylist = ["python", "sql", "mysql"]
mydict = { "name": "ABC", "job": "XYZ" }
pickle.dump(myint, output_file)
pickle.dump(mystring, output_file)
pickle.dump(mylist, output_file)
pickle.dump(mydict, output_file)
output_file.close()
with open("a.bin", "rb") as f:
while True:
try:
r=pickle.load(f) Read objects
print(r)
one by one
print(“Next data")
except EOFError:
break
f.close()
File Handling
Insert/append record (using
Dictionaries) in a Binary file
- pickle module
rollno = int(input('Enter roll number:'))
name = input('Enter Name:')
marks = int(input('Enter Marks'))

#Creating the dictionary


rec = {'Rollno':rollno,'Name':name,'Marks':marks}

#Writing the Dictionary


f = open('student.dat','ab') Here we are creating
pickle.dump(rec,f) dictionary rec to dump
f.close() it in student.dat file
File Handling
Read records (using Dictionaries) from a Binary
file
- pickle module
f = open('student.dat','rb')
while True:
try:
rec = pickle.load(f) Here we will iterate
print('Roll Num:',rec['Rollno']) using infinite while
print('Name:',rec['Name'])
loop and exit on end of
print('Marks:',rec['Marks'])
except EOFError: file is reached.at each
break iteration a dictionary
f.close() data is read into rec
and then values are
being displayed
File Handling
Search record in a Binary
file
f = open('student.dat','rb')
flag = False
- pickle module
r=int(input(“Enter rollno to be searched”))
while True:
try:
rec = pickle.load(f) Here value of r to be
if rec['Rollno'] == r: searched will be compared
print('Roll Num:',rec['Rollno']) with rollno value of file in
print('Name:',rec['Name']) each iteration/next record
print('Marks:',rec['Marks']) and if matches then
flag = True relevant data will be shown
except EOFError: and flag will be set to True
break otherwise it will remain
if flag == False: False and ‘No Record Found
print('No Records found') message will be displayed’
f.close()
File Handling
Update record of a Binary file
f = open('student.dat','rb') - pickle module
reclst = []
r=int(input(“enter roll no to be updated”))
m=int(input(“enter correct marks”))
Here we are reading all records from
while True:
try:
binary file and storing those in reclst
rec = pickle.load(f) list then update relevant roll no
lrecst.append(rec) /marks data in reclst list and create
except EOFError: /replace student.dat file with all
break data of reclst.
f.close() If large data are in student.dat file
for i in range (len(reclst)): then It’s alternative way can be
if reclst[i]['Rollno']==r: using temporary file creation with
reclst[i]['Marks'] = m
corrected data then using os module
f = open('student.dat','wb')
for x in reclst:
for remove and rename method
pickle.dump(x,f) (just similar to modification of text
f.close() file)
File Handling
Delete record of a Binary file
f = open('student.dat','rb')
- pickle module
reclst = []
r=int(input(“enter roll no to be deleted”)) Here we are reading all records from
while True: binary file and storing those in reclst
try: list then write all records except
rec = pickle.load(f) matching roll no(with the help of
reclst.append(rec)
continue statement) in student.dat
except EOFError:
file.Due to wb mode old data will be
break
f.close() removed.
f = open('student.dat','wb') If large data are in student.dat file
for x in reclst: then It’s alternative way can be
if x['Rollno']==r: using temporary file creation with
continue corrected data then using os module
pickle.dump(x,f) for remove and rename method
f.close() (just similar to deletion from text
file)
RANDOM ACCESS METHODS

All reading and writing functions


discussed till now, work sequentially in the file.
To access the contents of file randomly –
following methods are use.

seek method

tell method
seek method

seek()method can be used to position the


file object at particular place in the file.
It's syntax is :

fileobject.seek(offset [, from_what])
here offset is used to calculate the position
of fileobject in the file in bytes. Offset is added to
from_what (reference point) to get the position.
Following is the list of from_what values:
seek method

Value reference point

0 beginning of the file (for text files and binary files)


1 current position of file (works for binary files only)
2 end of file (works for binary files only)

default value of from_what is 0, i.e. beginning


of the file.
seek method

f.seek(7) keeps file pointer at 7th byte reads


the file content from 8th position
onwards to till EOF.
Reading according to size

In the input function if you specify the


number of bytes that many number of bytes
can be fetched and assigned to an identifier.
Reading according to size

f.read(1) will read single byte/ character


starting from byte number 8. hence byte
number 8 is P so one character/byte is fetched
and assigned to f_data identifier.
Reading according to size

f.read(2) - will read 2 chars/bytes


f.read(4) - will read 4 chars/bytes

and so on..
tell method
tell() method returns an integer giving
the current position of object in the file. The
integer returned specifies the number of bytes
from the beginning of the file till the current
position of file object.

It's syntax is

fileobject.tell()
tell() method
tell()method

tell() method returns an integer and


assigned to pos variable. It is the current
position from the beginning of file.
File
Handling
f = open("a.txt", 'w')
line = 'Welcome to python.mykvs.in\nRegularly visit python.mykvs.in'
f.write(line)
f.close()
OUTPUT
f = open("a.txt", 'rb+') 0
print(f.tell()) b'Welcome'
print(f.read(7)) # read seven characters
print(f.tell()) 7
print(f.read()) b' to
print(f.tell()) python.mykvs.in\r\nRe
f.seek(9,0) # moves to 9 position from begining
print(f.read(5))
gularly visit
f.seek(4, 1) # moves to 4 position from current location python.mykvs.in'
print(f.read(5)) 59
f.seek(-5, 2) # Go to the 5th byte before the end b'o pyt'
print(f.read(5))
f.close() b'mykvs'
b'vs.in'

You might also like