100%(1)100% found this document useful (1 vote) 2K views36 pagesChapter 5 Text and Binary File Handling
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here.
Available Formats
Download as PDF or read online on Scribd
5.8 Working with Binary Files
Working with CSV Files
uter programs work with files. This is because files help in storing information
Word processors create document files # Database programs create files of
files. So, we see itis the files
; Compilers read source files and generate executabl
ofl i itself sa bunch of aes stored on some
Tanguage offers some Proviso
worked with, inside programs: “/ if
ive etc. EV y programming
4in this chapter, you shall
ams. Python js no exception an
gh Python programs.COMPUTER SCIENCE WITH PYTHON
5.2 DATA FILES
The application, for later use
toa ic 8. The
data files are the files that store data pertaining specifi
data files can be stored in feo ways | as
| © Text files > te
1. Text Files ofa stream of ASCII or Unicode characters (1).
A text file stores information in the form in text files, each line of text is termin,
aetna Ren era ihe OF ie ating System) known as EOL (End of 1.
Monee nee Sana ternal translations take place when this EOL characteris rea
a ee default, this EOL character is the newline character (\\,))
bernie Rie contra (\r\n’)-
The text files can be of following types : ae
Regular les, These are the text files which store the text in the same form ,
2 typed. es character ends a line and the text translations take place. Th...
files have a file extension as -txt.
(i) Delimited Text files. In these text files, a specific character is stored to separate hy
values, ie,, after each value, e.g., a tab or a comma after every value.
© Whena tab character is used to separate the values stored, these are called TSV file,
(Tab Separated Values files). These files can take the extension as txt or coy
© When the comma is used to separate the values stored, these are called CSV files
(Comma Separated Values files). These files take the extension as .csv.
For instance, have a look at following example :
R 2 ontent: | am simple text. Cer
oe, F The CSV (Comma Separated Values) format
@ popular import and export format for
ene ex. spreadsheets and databases
Most commonly used delimiter in a CSV files
files (e.g., .INI files) and rich text format ®™ma(,), butt can also use other delim te:
files (RTF files) are also text files. Characters like tab (—>), pipe (|), tide
ete.
Iam > simple = text.
2. Binary Files
A binary file stores the information in the form of a stream of bytes. A binary file contai
information in the same format in which the information is held in memory, i, the file cont
that is returned to you is raw (with no translation or no specific encoding). In binary f
is no delimiter for a line. Also no translations occur in binary files. As a result, binary
faster and easier for a program to read and write than are text files. As long. as the file does
need to be read by people or need to be ported to a
different type of system, binary files are the best way to —ee7=—
store program information. Be co be opened In ary 124
The binary files can take variety of extensions. Most editor and are in human readable fo™ wa
non-text file extensions (any application defined inary fles are notin human ead30
extension) are binary files.
leNo tak a Python program, you need to open Hn 8 specie
a“
nr aan, at erm The te pt
petaiong
ata 10 Whe, which in turn inetd any one of
the first thing that
lowing he You do is open the file. It done using
> = open()
2 * OPEN , )
MyFile = open(“taxes.txt") <—— aad aad
le2 = open(“data.txt”, "r”)
ibove statement opens the file “data.txt” in read
to file object namely file2,
‘one more file-open statement :
"Filed = open("e:\main\\result. txt", "w') <—— Pret mi ok for th le
mode (because of “r’ given as mode) and
‘statement opens file “result.txt” (stored in folder
invite mode (because of" givens mode) nt
to file object namely file3, (Aeticonete ee: we
above file open statement, the file path contains Metandle, &
hes in place of single slashes.)
given three file-open statements must have raised these questions in your mind
is file-object?
is mode or file-mode ?
lines will have answers to all your questions, but for now, let us summarize the file
's open( ) function creates a file object which serves as a link toa file residing on yourCOMPUTER SCIENCE WITH PYTHOn,
) function is a path t
st the file name
le in the current
© The first parameter for the opent
the file you'd like to open. If
given, then Python searches for
folder.
© The second parameter of the open ee
corresponds to a mode which is typically read (")
write ('w’), or append (‘a’). If no second par
given, then by default it opens it in read (‘r) mode.
Figure 5.1 summarizes the open ) function of Python for you.
Fle object Path to file wie
£ = open(“cz\\temp\\data.txt”, "r')
Figure 5.1. Working of file open( ) function.
As you can see in Fig. 5.1, the slashes in the path are doubled. This is because the slashes have
special meaning and to suppress that special meaning escape sequence for slash i., \\ is
given.
However, if you want to write with single slash, you may write in raw string as :
with r here, you can give single. ————_.
elites paket f =open(r"“c :\temp\data. txt", “t") ie
The prefix r in front of a string
The prefix r in front of a string makes it raw string that makes it raw string that means
means there is no special meaning attached to any character. there is no special meaning
Remember, following statement might give you incorrect _ attached to any character.
oat! aE
f = open("c :\temp\data. txt”, "t") «__ ee
Reason being that ‘will be treated as tab character and ESSERE
‘\d’ on some platforms as numeric-digit or some error.
Thus the ftvo ways to give paths in filenames correctly are : re ee epee rode is read
ue pre pel Ta mode, ie, if you do not provide any
(i) Double the slashes e.g., file open mode, Python will open it
# = open("c:\\temp\ \data. txt", "r") eer.
(i) Give raw string by prefixing the file-path string with an r £8.
= open(r"c: \temp\data. txt", "r")
"éata,txt” rather than “c\\tem\data tt”Object/File Handle
are used 10 read and write date to 0 file
b d hw om dink.
object is used to obtain a reference to the file on disk ence’
it for a number of different tasks a ee ne
(also called file-handte) is very im
/ery Important and useful
as through a file-object only, a Python program can work
files stored on hardware, All the functions that you
non a data file are performed through file-objects
n you use file open( ), Python stores the reference of mentioned file in the file-objed.
abject of Python is a stream of bytes where the data can be read either byte by byte oF line BY
ine or collectively. All this will be clear to you in the coming lines (under topic ~ File access modes).
Access Modes
Python opens a file, it needs to know the file-mode in w!
de governs the type of operations (such as read or write or append)
{f refers to how the file will be used once it’s opened. File modes
hich the file is being opened. A
possible in the opened
supported by Python
Fe i Sean ge pea seed
% Default mode ; File must exist already, otherwise
Python raises I/O error.
© If the file does not exist, file is created.
@ If the file exists, Python will truncate existing data and
over- write in the file, So this mode must be used with
caution.
# File is in write only mode.
© If the file exists, the data in the file is retained and new
data being written will be appended to the end.
Ifthe file does not exist, Python will create a new file.
¢ File must exist otherwise error is raised.
Both reading and writing operations can take place.
# File is created if does not exist.
* If file exists, file is truncated (past data is lost)
& Both reading and writing operations can take place,
* File is created if does not exist.
* If file exists, file's existing data is retained
appended.
‘e Both reading and writing operations can take place
;mew data isCOMPUTER SCIENCE WITH Prnycy,
To create a file, you need to open a file in a
mode that supports write mode (ie, ‘Ww’, Ota’ OF
“we or ‘ae modes),
5.3.2
Closing Files ject. Closing of file is itnpor,
An open file is closed by calling the cfose(,) method of its flestiet one |
In Python, files are automatically closed at the end of the program a system may not chon :
the habit of closing your files explicitly. Why ? Well, the opera i ees
data out tothe file uni itis close (this can boost performance). What fh PO Gel have 1
Program exits unexpectedly there is a danger that your precious .close( ) : feen eo
and the file on the dic,
For instance, if a file Master.txt is opened via file-handle ilose( ), no tasks can be
; 0 ; aR
outfile, it may be closed by the following statement : hs that — th
outfile.close( ) <—— ee aoe
Please remember, open( is a built-in function (used standalone) while close( ) is a method use:
with file-handle object.
5.4 WORKING WITH TEXT FILES
Python provides many functions for reading and writing the open files. In this section, we are
going to explore these functions. Most common file reading and writing functions are being
discussed in coming lines.
5.4.1 Reading from Text Files
Python provides mainly three types of rad functions to read from a data file, But before you ca
read from a file, the file must be opened and linked via a file-object or file handle. Most
common file reading functions of Python are listed below in Table 5.2.
Table 5.2 Python data files — reading writing functions
‘SNo.| Method | ‘Syntax Pe Mieerpen E
1. | read() .read([{n]) reads at most n bytes ; if no # is Specified, reads
the entire file,
Returns the read bytes in the form of a strin
>>> filel = open("E:\\mydata\ \info. txt
>>> readInfo = filel.read(15)
>>> print(readInfo
It's time to wo 15 bytes read
>>> type(neadInfo)
str > Fllel = open(*E:\\mydata\\info.txt")
>»? readinfo = filel.readline()
>>> print(readtnfoy
It's time to work with files,
reads all lines and returns them in a list
>>> file = open("e:\\mydata\\info. txt")
>>? readinfo = filel.readline() |
>>> print(readinfo)
|
m4
|
("Te's time to work with files.\n", “Files offer and
fan ant Power to store your work/data/informaticn |
for later use.\n", “siaply create a file and
Share(write) tn At \n*, “Or open an existing File |
and read from i.\n"]
|
>>> type(neadinfo) |
iehar ce» in above syntaes is the fil-objet holding open He’ veloreac, |
Oe ako nanples now. For the examples and explanations below, we are using a
poem. txt storing the content shown in Fig, 5.2
WHY 7
‘We work, we try to be better
‘We work with full zest
But, why Is that we just don't know any letter
We still give our best.
We have to steal,
But, why is that we still don't get a meal
‘We don't get luxury,
We don't get childhood,
But we still work,
Not for us, but forall the others.
Why is it that some kids wear shoes, BUT we make them ?
by — Mythil, class §do not want to have the newline characters inserted by prin statement in the output then
argument inthe end of prin) statement so that print does not put any additional
' nt in the output. Refer to code snippets 3 and 4 below.
3 Reading a file's entire content
myfile = open(r'é :\poem.txt’, "r”)
str=myfile.read()«— '
eet aoe
myfile.close()
s output produced by above code is : car
You can also combine the open} and read)
functions as:
File("filenane”, ) read )
We work, we try to be better Shed ation bse one
We work with full zest —
But, why is that we just don’t know any letter.
We still give our best.
We have to steal,
‘But, why is that we still don't get a meal.
| We don't get Tuxury,
| We don’t get childhood,
| But we still work,
Not for us, but for all the others.
Why is it that some kids wear shoes, BUT we make them ?
by mythili, class 5
myfile = open(r'E:\poem.txt', "r")
str = myfile.readline()
print(str, end="")
str = myfile.readline()
Petes Oe tL Acree Argamet end “at the end, Of prini() statement
str =myfile.readline() ‘will ensure that output shows exact content of the
ve data file and rint-inserted newline characters are
print(str, end="") Re
myfile.close()
ee ie ee ie HS TE eafor loop’s variable moves through the file line by line where a line ofa file is considered as @
“sequence of characters up to and including a special character called the newline character ('\r).
_Sothe for loop's variable starts with first ine and with each iteration, it moves to the next line. As
the for loop iterates through each line of the file the loop variable will contain the current line of
_the file as a string of characters,
& Displaying the size of a file after removing EOL characters, leading and
trailing white spaces and blank lines
myfile = open(ré:\poem. txt’, "e")
stri="" #initially storing a space (any non-None value)
size=0
tsize=0
while stra :
str1 = myfile. readline()
tsize = tsize+len(str1)
size = size + len(stri.strip())
print ("Size of file after renoving all EOL characters & blank lines:", size)
print ("The TOTAL size of the file:", tsize)
myfile.close()
e output produced by the above code fragment is:
Size of file after removing all EOL characters & blank lines : 360
‘The TOTAL size of the file : 387
mmyfile = open(E:\poen.txt’, "e")
‘S$ =myfile.readlines() *———— noice itis readlines( ) not readline )
print(s)
myfile.close()
look at the output. The readlines( ) has read the entire file in a list of strings
‘wHY?\n", '\n', ‘We work, we try to be better\n', ‘we work with
zest\n', “But, why is that we just don’t know any letter.\n",
', "We still give our best.\n’, ‘we have to steal,\n', “But, why
we still don’t get ameal.\n”, '\n', “we don’t get luxury,
don’t get childhood,\n”, ‘But we stil] work,\n', ‘Not for
for all the others.\n', ‘\n', ‘why is it that some kids wear
we make them ?\n', by MythiTi, class 5\n']puTER SCIENGE WITH PYTHON ~ 1)
Now that you ae fanstir with these reading functions’ working let we write some Programs
P 1 Wrive « program to display the sie of a file in bytes
myfile © open(ré:\poem, txt’, "r")
str =myfile.read() rr.
size = len(str) size of the given file poem.txt is
print ("Size of the given File poem. txt 1s") 387 bytes
print(size, "bytes")
‘Write a program to display the number of lines in the file
myfile = open(rE:\poem.txt’, "r")
= myfile.readlines()
Linecount = len(s)
print ("Number of Lines in poem.txt is", linecount)
myfile.close()
>>>
number of lines in poem.txt is 18
4.2 Writing onto Text Files
After working with file-reading functions, let us talk about the writing functions for data files
"available in Python. (See Table 5.3) Like reading functions, the writing functions also work on
‘open files, ie,, the files that are opened and linked via a file-object or file-handle.
Python Data Files ~ Writing Functions
ite() _| .write(str1)
writes all strings in list L as lines to file refe
renced by
en you open a file in “w” or write mode, Python overwrites an existing file or creates 0
g file. That means, for an existing file with the same name, the earlier data gets lost
wer, you want to write into the file while retaining the old data, then you should open
“a” or append mode. A file opened in append mode retains its previous data while
add newer data into. You can also add a plus symbol (+) with file read mode to
the file has been opened in append mode (“a”) to retain the old content.
f the file has been open in ‘r+’ or ‘a+’ modes to facilitate reading as well as writing(9) ifthe file has been opened in write-only mode (“w”)
11) ifthe file has been open in ‘w+’ mode to facilitate writing as well as reading
Make sure to use close() function on file-object after you have finished writing as
‘sometimes, the content remains in memory buffer and to force-write the content on file
and closing the link of file-handle from file, close( ) is used.
at us consider some examples now.
8 Create a file to hold some data
Fileout = open("Student dat", “w")
for i inrange(5) :
Name = input ("Enter name of student :")
fileouturste(nane)
aay
fileout.close() The write() will simply write the content in file
without adding any extra
Its important to use close()
ate :
Enter name of student : Riya
r name of student : Rehan
Enter name of student : Ronag
"i Enter name of student : Robert
Enter name of student : Ravneet.
an see the file created in the same folder where this Python script/program is saved
53(a) below). However, if you want to create the file in specific folder then you must
the file-path as per the guidelines discussed earlier,
ou can open the created file (“student.dat” in above case) in Notepad to see its contents,
b) shows the contents of file created through code snippet 5.
The data you enterec is part
‘Of the file. BUT notice that
write) does not add anewine
character on its own
(0) File created through open( ) with “w" mode is stored in the same folder as that of script fle
(0) The write( ) function does not add any extra character in the file,COMPUTER SCIENCE WITH PYTHON — x
Code Snippet 9 Create a file to hold sonte data, separated us lines ;
—_— ith code snippet 8)
(This code is creating a different file than created with code ®tP!
fileout = open("studenti. txt”, “W")
for i in range(5) :
name = input ("Enter name of student :")
Fileout .write(name) . ein
“yn? written afer
#Lleout..write(’\nt) <—— Te newline charter“
fileout.close()
‘The sample run of the above code is as shown
below :
>
Enter name of student : Jaya
newline characters at the Enter name of student : Jivin
ee enter name of student : Jonathan
you added a newine “esa
after Enter name of student : Jagjee
every name
Enter name of student : Javed
aah
Sonachen ‘See this time the file has.
The file created by code snippet 9 is shown above.
Code snippet 10 Creating a file with some names separated by newline characters without usin
TTC Pe function:
(For this, we shall use writelines( ) inplace of write() function which writes the content
ofalist to afile. Function writelines() also, does not add newline character, so you have t
take care of adding newlines to your file.)
fileout = open("Student3. txt", "w')
List =[]
for i in range(5) :
nane = input ("Enter name of student :")
Uist append(name +"\nt) #—_
Responsibility to add newline
fileout .writelines(List1) shharacter is of programmer
Fileout .close()
Sample run of the above code is as shown below
i Nitya
+ Noor
3 Nathan
+ Naved
2 Navinare familiar with these writing functions’ working, let us write some programs
‘on the same,
Wee @ program to get roll numbers, na
‘store these details in a file called *Marks.ox
and marks of the students af a clase (wet from usec) aed
count * Ant (input ("How many students are there in the ¢
FAleout = open (Marks, txt", "w")
95?"))
for 1 in range(count) :
print(“Enter details for student", (141), "below:")
Pollno = int(input("Rolino: ))"
name = input("Name ;")
marks = float(input ("Marks :"))
rec = str(rollno) +","+name +","+ str(marks) +\n"
fileout .write(rec) Joining individual information by
#ileout .close() SST ein come Be
y students are there in the class? 3
Fle Edit Format, View Help
|32,haze1,67.75 z
45, diya, 1
| 16, Noor, a
File created by
ks : 78.5
ter details for student 3 below :
Ino: 16 Figure 5.4 file created through program.
carefully notice, we have created comma separated values in one student record while
in file, So we can say that the file created by program 5.4 is in a format similar to CSV
separated values) format or it is a delimited file where comma is the delimiter
Write a program to add two more students’ details to the file created in program 5.3
a ks .txt" see ees Notice the file is opened in append mou
ge ren i ) (“a”) this time so as to retain old
for i in range(2) : We want to ad
print (“Enter details for student", (i+1), "below :") Ei"
rollno = int(input(“Rollno:"))
name = input("Name :")
marks = float (input ("Marks:"))
rec = str(rollno) +","+name+"," + str(marks) +\nt
fileout .write(rec)
.close()‘enter a
Rolling ;
Name: saat
marks : 78.9
Enter ee for
RolIno ;
Name : sk
Marks : 89.5
for student 1 below :
‘Student 2 below :
on + created through »
P 5.5 Write a program {0 display the contents of file “Marks.tat”
c Outpet
rogram fileinp = open(Marks. txt", "r" ‘
while str : ‘
Str = fileinp.readline() &
print(str) ,
fileinp.close()
Have a look at some
text file Fig, 5.5)
File : Answer.txt
Letter ‘a is a wonderful letter.
{tis impossible to think of a sentence without it,
We know this will never occur,
How mediocre our world would be without this single most powerful letter.
Figure 5.5 The contents of file Answer.txt.
‘rite a program to read a text file line by line and di
myfile = open("Answer.txt", "
line ="*
while line :
Line = myfile, readline() #one line read from file
# printing the Line word by word using split()
for word in line. split():
Print(word, end = 'g')
print()
close the file
myfile.close()
i ‘S#atwonder ful: #etter.¢
blePeoe think
isplay each word separated by a
n)
#initially stored a ‘Space (a non-Non, alue@ program to read a text file and display the count of vowels and consomants i the file.
‘= open("Answer.txt", "r")
initially stored a space (a non-None value)
variable to store count of vowels
variable to store count of consonants
Muhile ch stores a Non-None value
ch=myfile.read(1) #one character read from file
Mfchin{'a', 'A', ‘e', 'E', 4, 'T', ‘0°, 0", ‘u's U'Te
vcount = vcount +1
else:
count = ccount + 1
("Vowels in the file :", vcount)
‘Consonants in the file :", ccount)
‘the file
.close()
ish( ) Function
5u write onto afile using any of the write functions, Python holds everything to write in
buffer and pushes it onto actual file on storage device a later time. If however, you
Python to write the contents of buffer anto storage, you can use flush() function.
matically flushes the file buffers when closing them ie, this function is implicitly
the close( ) function. But you may want to flush the data before closing any file, The
lush( ) function is :
The flush( ) function forces the |
writing of data on disc still)
pending in output buffer. |
n( ‘out. Log’, 'w+")
(‘The output is \n')
( "My" + "work-status "+" is")
With this statement, the strings written so far,
gC The ups and My wor is
have been pushed on to actual file on disk
ea These write statements’ strings may still
be pending to be writen on to disk
+ Tie flush( ) function ensures that whatever is held in
‘Output buffer, is written on to the actual file on disk
d data from file and return it in string
where each line is one
d readline( ) functions discussed above, rea
»() function returns the entire file content ina listCOMPUTER SCIENCE WATH FTHON x
stespaces Le, spaces oF tabs or
All these read functions also read the leading and trailing ‘and leading whitespaces, yo.
newline characters. Ifyou want to remove any of these ak EN
can use strip() functions [rstrip(), Istrip( ) and strip( )} &8
Recall that :
© the strip( ) removes the given character from both
© the rstrip( ) removes the given character from trailing
© the lstrp( ) removes the given character from leading end #4
To understand this, consider the following examples :
1. Removing EOL ‘n’ character from the line read
fh=File("poem.txt, "e") Will remove the end of line
Line = fh.readline() 3
line = Line.rstrip(‘\n')
ends.
end i, right end.
left end.
from the file.
newline character ‘\”"
2. Removing the leading whilespaces from
the line read from the file
line = file("poem.txt, "r") readline()
line = line. Istrip()
Now can you justify the output of following
code that works with first line of file poem.text
shown above where first line containing leading,
8 spaces followed by word ‘WHY? and a’\n' in
the end of line.
Following lines list the steps that need to be followed in the order as mentioned below. The five steps (0 use
files in your Python program are :
1. Determine the type of file usage
Under this step, you need to determine whether you need to open the file for reading purpose (input type «
‘usage) or writing purpose (output type of usage). Ifthe data is to be brought in froma file to memory, then
file must be opened in a mode that supports reading such as “r® or “r+” etc.
‘Similarly, if the data (after some processing) is to be sent from memory to file, the file must be opened
‘mode that supports writing such as “w” or “w+” or “a” etc,
2. Open the file and assign its reference to 2 file-object or file-handle
Next, you need to open the file using open ) and assign itto a file-handle on which all the file-operations ».
be performed, Just remember to open the file in the file-mode that you decided in step 1.
3. Now process as required ¢
As per the situation, you need to write instructions to process the fleas desited. For example, you mish
to open the file and then read it one line at a time while making. ‘computation, and so on.
4. Close the file A
This is very important step especially if you have opened the file in write mode, This is hec:
the last lap of data remains inbutfer ands not pushed onto disk unl a close ) operation i prtificance of File Pointer in File Handling
ae ean which tells the current position in the file where writing or
place. (A file pointer in this context works like a book-mark in a book).
oo from the file or write onto a file, then these two things happen
“this Operation takes place at the position of file-pointer and | To see \
filepointer advances by the specified number of bytes ce \
5.6 illustrates this process, Its important to understand how a a)
te works in Python files, The working of file-pointer has
described in Fig. 5.6. i
EL EEbE bb bh ebb
an of ile.pointer when file is opened in read mode (*r*), ie, file-pointe les in beginning
14 byte from the file from the position the file-pointer is currently at ; and the file pointer advances by one byte.
ow the ch will hold “1” and the file pointer will be at next byte holding value ‘2’ (see below)
lst Ts. DTT Pelsl [shell Js).
the file-pointer has advanced by 1 position as previous read operation read I byte only.
‘nexd read will take place at the current position of file-pointer.
Feith [i
from the fle from the position the file-pointer is currently at and the file pointer advances by 2 bytes,
‘str will hold ‘2, and the file pointer will be at next byte holding value 'H’ (see below)
PELEEEL TT) Tshels[]. 1s)
_ See now, the file-pointer has advanced by 2 positions as previous read operation read 2 bytes only
“Now the next read will take place at the curent position of ile-pointer
ow
1}, }eol7|-|7|5|\"
Figure 5.6 Working of a file-pointer.
and the Opening Position of File-Pointer
n ofa file-pointer is governed by the filemode it is opened in. Following table lists the
on of a file-pointer as per filemode.
the a
beginning of the file (Overwrites the file if the fil
Tr if the file exists otherwise creates a new filefor files is very handy when you have two related operations which you'd lke to
‘with a block of code in between, The syntax for using with statement is :
i
hopen(, ) as +
File manipulation statements»
‘example is opening a file, manipulating the file, then closing it :
hopen(‘output.txt’, 'w) as f:
f.write('Hi there!')
at with statement will automatically close the file after the nested block of code. The advantage of
j awith statement is that itis guaranteed to close the file no matter how the nested block exits. Even if an
(a runtime error) occurs before the end of the block, the with statement will handle it and
and Relative Paths
name of a file or directory or folder consists of path \ primaryname.extension
1 sequence of directory names which give you the hierarchy to access @ particular
ry or file name. Let us consider the following directory structure :
¥ omen
(root folder) the
top most folder
rs) on)
=
eaees) ==
oo =
e-letter: \directory {\directory...]
, ‘refers to root directory and other (’\’s) separate a director
ry name fromeR asWITH BINARY FILES,
ow you have leamt to write lines/strings and lists on files. Sometimes you may need to
and read non-simple objects like: dictionaries, tuples, lists or nested lists and so forth on to the
Since objects have
E Some structure or hierarchy associated, it is important that they are
‘stored in way so that their structure/hierarchy is maintained. For tl
this purpose, objects are often
serialized and then stored in binary files,
© Serialisation (also called
into a byte stream so that
__ Stream in such a way
a ti
Pickling) is the process of converting Python object hierarchy
tit can be written into a file, Pickling coverts an object in byte
that it can be reconstructed in original form when unpickled or
‘© Unpickling is the inverse of P
hierarchy. Unpickling produce
‘ickling where a byte stream is converted into an object
s the exact replica of the original object.
Python Provides the pickle module to achieve this. As per CEs
Python's documentation, “The pickle module implements a “Pickling” isthe process whereby
Fundamental, but power algorithm for seriatizing and aa ee
de-serializing a Python object structure.” Inthis section, you shall" “unplekling” is the inverse
n fo "use pickle module for reading/writing Objects in operation, whereby a byte-|
ary files.
‘stream is converted back into an
object hierarchy.
And then, you may use dump() and load() methods!
Of pickle module to write and read from an
en binary file respectively. Process of working with binary files is similar to as you have been
So far with a little difference that you work wit
ith pickle module in binary files, ie,
Import pickle module.
— Amport pickle
ling/Closing Binary Files
file is opened in the same way
Dut make sure to use “b”
as you open any other file (
(as explained in section 5.3
with file modes to open a file
in binary mode e.g,
. — Binary file opened in write mode with file handle as
Dfile
"* Notice ‘is used with the fle modes
tapaggee Binary file opened in read mode with fle handle as Filet
oad ) of pickle modu but
for an open binary file. But as
pes
these serialise/de-serialise objects in string
per syllabus, we shall only cover only load{ )ears
[ae rego ty is a
COMPUTER SCIENCE WITH PYTHON — x
output file mode and it does not
Like text files, a binary file will get created when opened in an out
opened i he file will get created if it does
exist already, That is, for the file modes, “w, “w +", “A ee and “w +” will overwrite
not exist already but ifthe file exists already, then the oe nic
the file and the file mode “a” will retain the contents of any other file ie, as
An open binary file is closed in the same manner as you close any
Ofile.close() ite/read into binary files.
Let us now leam to work with pickle module’s methods to write)
e
Es eT ee on in,
We shall talk about both these methods (reading inside try cone 6
statement) when we talk about reading from binary files in section 5.6.3.
5.6.2 Writing onto a Binary File — Pickling
E
In order to write an object on to a binary file opened in the write mode, you should use dump\)
function of pickle module as per the following syntax :
Pickle. dump(, )
For instance, if you have a file open in handle file1 and you want to write a list namely list! in
the file, then you may write :
Pickle.dump(1ist1, file1) +————_. objet listt is being written on file opened with file handle as Filet
Inthe same way, you may write dictionaries, tuples or any other
Python object in binary file using dump() function.
For instance, to write a dictionary namely student! in a file
open in handle file2, you may write :
pickle. dump(student1, Oe) ae
Object studentt is being written on
file opened with file handle as File2
llows you to pickle object.
following data types
Integers, Floats, Complex
Strings, Tuples, Liss, Sets
‘containing picklable
and classes’ objects e%:
Now consider some example programs given below.
5.8
m
Write a program to a binary ?
Snpleyees tree eRe employe ce
import pickle
# dictionary objects tobe stored in the binary File
empl = {"Empno’ : 1201, ‘Name* :
enp2 = {‘Enpno' : 1221, ‘Name
emp3 = {‘Empno’ : 1251, ‘Name
emp = {"Empno" : 1266, "NameFIUE HANDUNG a1
#open file in write mode
fenpFie = open( imp. dat', yh!) ——— Se Pr write md ad for the Wary fle
#write onto the file
-_pckle. dump (emp, enpfi.te) Mg
Berssiereem2, enotile) |. —————-— Dighany ote rg ert fe aed
Pickle.dump(emp3, empfile) ane ae
pickle.dump(emp4, enpfile)
print( “Successfully written four dictionaries”)
‘empfile.close( )
# close file
___ Theabove program will create a file namely Emp.dat in your program’s folder and if you try to
‘open the created file with an editor such as Notepad, it will show you some garbled values (as
__ shown below) because binary files are not in human readable form.
9 Write a program to get student data (roll no., name and marks) from user and write onto a binary fie
‘The program should be able to get data from the user and write onto the file as Jong as the user wants.
import pickle
stu={} # declare empty dictionary
stufile = open('Stu.dat', 'wb') # open file
__ #get data to write onto the file
ans='y'
no = int(input("Enter roll number : "))
‘name = input("Enter name : ")
marks = float(input("Enter marks : ")) Output
#add read data into dictionary Prt eice\ fumber:+ 12
cae enter rol] number :
uae Enter name : sia
‘Name’ ] = name Enter marks : 83.5
want to enter more records? (y/n)...¥
into the file Enter rol] number : 12
ners nM
1 dump(stu, stufile) Enter name : Guneet
beanie Fic lganter: ») Enter marks : 80.5
i Snel) ie als a Gad") ant to enter more records? (y/n)-..Y
close e
Enter roll number : 13
Enter name : James
Enter marks : 81
want to enter more records? (y/n)..-n
ene in OED
\
‘These 3 student records are
written to the file studat5.6.24 Appending Records in Binary Files
Appendling records in binary files is similar to waiting, only
thing you have to ensure is that you must open the file if
append mode ("ab"), A file opened in append mode will
retain the previous records and append the new
‘written in the file. Just as you normally write in a binary file,
You write records while appending using the same duumpt )
funetion of the pickle module,
P B10 Wee a program to append sudo reanl 0g een A Tene nr” _
use
rogram
inport pickle
‘declare empty dictionary
stu={}
‘# open file in append mode
stufile open("Stu.dat’, "ab") <————— for appending the records, the fil is opened
# get data to write onto the file roped made as of he regan sar
ein ‘that of writing records.
ans = 'y
» while ans =="y! :
| no = int (input("Enter roll number : "))
| name = input ("Enter nane :")
marks = float( input("Enter marks > ")
1 # add read data into dictionary
stuf RolIno'] = rno
stu['Name"] = name
stu[ 'Marks"] = marks
# now write into the File
pickle.dunp(stu, stufile)
ans = input ("Want to append more records? (y/n)
#close file
stufile.close()
‘The sample run of the above program is as shown below :
Enter rol] number : 14
Enter name : Ali
Enter marks : 80.5 1 more student record is appended to te ile ss.
| want to append more records? (y/n)...n
5.6.3 Reading from a Binary File ~ UnPickling
Once you have written onto a file using pickle module’s dump() (as we did in the
section), you need to read from the file using load() function of the pickle modu
then unpickle the data coming from the file, .toread an object in nemp from afile open i ile-handle fout, you would write:
HRP = DACKL. Sad (Foust) —— and fm th le cpa th le hn ft ase
the read data in an object namely memp
reee tra att does the same for you. It reads the objects written by program 5.8 from the
and displays them, But before the program 5.11, read the following box. (Important)
{You move onto the prograin ode, iis impo
| raise EOF Error (a run time exception) when
you reach end-of-file while reading from the file.
‘handle this by following one of the below given two methods,
Use try and except blocks © Using with statement
Use try ond except blocks
- _—— In the try block, write the pickleload( )