Advanced Python Programming (UNIT-4) | 4321602
Unit-IV: Files Handling (Marks-14)
Till now, we were taking the input from the console and writing it back to the console to interact
with the user.
Users can easily handle the files, like read and write the files in Python. In another programming
language, the file-handling process is lengthy and complicated. But we know Python is an easy
programming language. So, like other things, file handling is also effortless and short in Python.
Sometimes, it is not enough to only display the data on the console. The data to be displayed may be
very large, and only a limited amount of data can be displayed on the console since the memory is
volatile, it is impossible to recover the programmatically generated data again and again.
The file handling plays an important role when the data needs to be stored permanently into the file.
A file is a named location on disk to store related information. We can access the stored information
(non-volatile) after the program termination.
4.1 INTRODUCTION TO FILES
We have so far created programs in Python that accept the input, manipulate it and display the
output. But that output is available only during execution of the program and input is to be entered
through the keyboard.
This is because the variables used in a program have a lifetime that lasts till the time the program is
under execution.
What if we want to store the data that were input as well as the generated output permanently so that
we can reuse it later? Usually, organizations would want to permanently store information about
employees, inventory, sales, etc. to avoid repetitive tasks of entering the same data.
Hence, data are stored permanently on secondary storage devices for reusability. We store Python
programs written in script mode with a .py extension.
program is stored on the secondary device as a file. Likewise, the data entered, and the output can be
stored permanently into a file.
What is a file? A file is a named location on a secondary storage media where data are permanently
stored for later access.
4.1.1 TYPES OF FILES
Computers store every file as a collection of 0s and 1s i.e., in binary form. Therefore, every file is
basically just a series of bytes stored one after the other.
There are mainly two types of data files — text file and binary file.
A text file consists of human readable characters, which can be opened by any text editor.
Binary files are made up of non-human readable characters and symbols, which require specific
programs to access its contents.
1. Text file
A text file can be understood as a sequence of characters consisting of alphabets, numbers and other
special symbols.
Files with extensions like .txt, .py, .csv, etc. are some examples of text files.
When we open a text file using a text editor (e.g., Notepad), we see several lines of text. However,
the file contents are not stored in such a way internally. Rather, they are stored in sequence of bytes
consisting of 0s and 1s.
INFORMATION TECHNOLOGY [Mrs. Aesha K. Virani , Mrs. Hina S. Jayani] 1
Advanced Python Programming (UNIT-4) | 4321602
In ASCII, UNICODE or any other encoding scheme, the value of each character of the text file is
stored as bytes. So, while opening a text file, the text editor translates each ASCII value and shows
us the equivalent character that is readable by the human being.
For example, the ASCII value 65 (binary equivalent 1000001) will be displayed by a text editor as
the letter ‘A’ since the number 65 in ASCII character set represents ‘A’.
Each line of a text file is terminated by a special character, called the End of Line (EOL).
For example, the default EOL character in Python is the newline (\n). However, other characters can
be used to indicate EOL.
When a text editor or a program interpreter encounters the ASCII equivalent of the EOL character, it
displays the remaining file contents starting from a new line.
Contents in a text file are usually separated by whitespace, but comma (,) and tab (\t) are also
commonly used to separate values in a text file.
2 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.
What is Differentiate between Text file and Binary file? SUMMER-2023,WINTER-2022
Binary File Text File
The content is encrypted and is not human-
The data is not encrypted and is human-readable.
understandable.
Used to store custom and compact data like User-friendly data is stored in text files mostly
images, audio, and text files in a single file. plain text.
The alphabet, digits, and special symbols are
Data in binary files are stored in binary format
stored as characters per byte. For example, the
(1s and 0s) to represent custom data, occupying
number 88732 is an integer that occupies 4 bytes
the same amount of memory as the number of
in the disk but will take up 6 bytes, 1 for each
bytes
digit in text files.
In the binary file, the conversion of a newline to Every newline character is first converted to a
carriage return-line feed combination is not carriage return-line feed combination and vice
done. versa before and after being written to the disk.
Need supporting custom applications or software Any simple text editor is enough to view the
to view the data. information in text files.
INFORMATION TECHNOLOGY [Mrs. Aesha K. Virani , Mrs. Hina S. Jayani] 2
Advanced Python Programming (UNIT-4) | 4321602
Files keep track of the end of the file from the A unique symbol of ASCII value 26 is used to
number of characters present. mark the end of the file in the text files.
What kind of different operations we can perform in a file? SUMMER-2022
Give list of file modes. Write Description of any four mode. SUMMER-2022
What is file handling? List files handling operation and explain it. SUMMER-
2022,SUMMER-2023
4.2 Files Handling
A file is a container in computer storage devices used for storing data.
When we want to read from or write to a file, we need to open it first. When we are done, it needs to
be closed so that the resources that are tied with the file are freed.
Hence, in Python, a file operation takes place in the following order:
o Open a file
o Read or write (perform operation)
o Close the file
4.2.1 Opening a file
A file operation starts with the file opening. At first, open the File then Python will start the
operation.
File opening is done with the open() function in Python. This function will accepts two arguments,
file name and access mode in which the file is accessed.
When we use the open() function, that time we must be specified the mode for which the File is
opening.
The function returns a file object which can be used to perform various operations like reading,
writing, etc.
File_object = open(File_Name,Access_Mode)
The file should exist in the same directory as the python program file else, the full address of the
file should be written in place of the filename.
The access_mode is an optional argument that represents the mode in which the file has to be
accessed by the program. It is also referred to as processing mode. Here mode means the operation
for which the file has to be opened like for reading, for writing, for both reading and writing, for
appending at the end of an existing file
The default is the read mode. In addition, we can specify whether the file will be handled as binary ()
or text mode.
By default, files are opened in text mode that means strings can be read or written.
Files containing non-textual data are opened in binary mode that means read/write are performed in
terms of bytes.
INFORMATION TECHNOLOGY [Mrs. Aesha K. Virani , Mrs. Hina S. Jayani] 3
Advanced Python Programming (UNIT-4) | 4321602
The files can be accessed using various modes like read, write, or append. The following are the
details about the access mode to open a file.
SN Access Description
mode
1 r r means to read. So, it opens a file for read-only operation. The file pointer exists
at the beginning. The file is by default open in this mode if no access mode is
passed.
2 rb It opens the file to read-only in binary format. The file pointer exists at the
beginning of the file.
3 r+ It opens the file to read and write both. The file pointer exists at the beginning of
the file.
4 rb+ It opens the file to read and write both in binary format. The file pointer exists at
the beginning of the file.
5 w It opens the file to write only. It overwrites the file if previously exists or creates a
new one if no file exists with the same name. The file pointer exists at the
beginning of the file.
6 wb It opens the file to write only in binary format. It overwrites the file if it exists
previously or creates a new one if no file exists. The file pointer exists at the
beginning of the file.
7 w+ It opens the file to write and read both. It is different from r+ in the sense that it
overwrites the previous file if one exists whereas r+ doesn't overwrite the
previously written file. It creates a new file if no file exists. The file pointer exists
at the beginning of the file.
8 wb+ It opens the file to write and read both in binary format. The file pointer exists at
the beginning of the file.
9 a It opens the file in the append mode. The file pointer exists at the end of the
previously written file if exists any. It creates a new file if no file exists with the
same name.
10 ab It opens the file in the append mode in binary format. The pointer exists at the end
of the previously written file. It creates a new file in binary format if no file exists
with the same name.
11 a+ It opens a file to append and read both. The file pointer remains at the end of the
file if a file exists. It creates a new file if no file exists with the same name.
12 ab+ It opens a file to append and read both in binary format. The file pointer remains
at the end of the file.
Example:
INFORMATION TECHNOLOGY [Mrs. Aesha K. Virani , Mrs. Hina S. Jayani] 4
Advanced Python Programming (UNIT-4) | 4321602
It is a read operation in Python. We open an existing file with the given code and then read it. The code is
given below –
#opens the file file.txt in read mode
f = open("file.txt","r")
if f:
print("file is opened successfully")
O/P:
file is opened successfully
In the above code, we have passed filename as a first argument and opened file in read mode as we
mentioned r as the second argument. The f holds the file object and if the file is opened successfully, it will
execute the print statement
Opening a file using with clause
In Python, we can also open a file using with clause.
The syntax of with clause is: with open (file_name, access_mode) as file_ object:
The advantage of using with clause is that any file that is opened using this clause is closed automatically,
once the control comes outside the with clause. In case the user forgets to close the file explicitly or if an
exception occurs, the file is closed automatically.
Example:
with open(“myfile.txt”,”r+”) as myObject:
content = myObject.read()
Here, we don’t have to close the file explicitly using close() statement. Python will automatically close the
file.
4.2.2 Closing a file
Once we are done with the read/write operations on a file, it is a good practice to close the file.
Python provides a close() method to do so.
While closing a file, the system frees the memory allocated to it.
The syntax of close() is:
file_object.close()
Here, file_object is the object that was returned while opening the file. Python makes sure that any
unwritten or unsaved data is flushed off (written) to the file before it is closed.
Hence, it is always advised to close the file once our work is done. Also, if the file object is re-
assigned to some other file, the previous file is automatically closed.
Example:
# opens the file file.txt in read mode
INFORMATION TECHNOLOGY [Mrs. Aesha K. Virani , Mrs. Hina S. Jayani] 5
Advanced Python Programming (UNIT-4) | 4321602
f = open("file.txt","r")
if f:
print("The existing file is opened successfully in Python")
#closes the opened file
f.close()
After closing the file, we cannot perform any operation in the file. The file needs to be properly closed. If
any exception occurs while performing some operations in the file then the program terminates without
closing the file.
We should use the following method to overcome such type of problem.
Example:
try:
f = open("file.txt")
# perform file operations
finally:
f.close()
Explain readline() and writeline() functions of the file. WINTER-2022
4.2.3 Writing to a Text File
For writing to a file, we first need to open it in write or append mode.
If we open an existing file in write mode, the previous data will be erased, and the file object will be
positioned at the beginning of the file.
On the other hand, in append mode, new data will be added at the end of the previous data as the file
object is at the end of the file.
w: It will overwrite the file if any file exists. The file pointer is at the beginning of the file.
a: It will append the existing file. The file pointer is at the end of the file. It creates a new file if no file
exists.
After opening the file, we can use the following methods to write data in the file.
write() - for writing a single string
writelines() - for writing a sequence of strings
for Write Method using ‘w’ mode:
Example:
# open the file.txt in append mode. Create a new file if no such file exists.
INFORMATION TECHNOLOGY [Mrs. Aesha K. Virani , Mrs. Hina S. Jayani] 6
Advanced Python Programming (UNIT-4) | 4321602
f = open("file2.txt", "w")
# appending the content to the file
f.write(''''''''Python is the modern programming language. It is done any kind of program in shortest way.''')
# closing the opened the file
f.close()
O/P:
File2.txt
Python is the modern programming language. It is done any kind of program in shortest way.
for Write Method using ‘a’ mode:
Example:
#open the file.txt in write mode.
f = open("file2.txt","a")
#overwriting the content of the file
f.write(" Python has an easy syntax and user-friendly interaction.")
#closing the opened file
f.close()
O/P:
File2.txt
Python is the modern programming language. It is done any kind of program in shortest way. Python has
an easy syntax and user-friendly interaction.
Writelines(argument) method in python:
f=open("names.txt", 'w')
list=["Ramesh\n" ,"Arjun\n", "Senthil\n", "Vignesh"]
f.writelines(list)
print("List of lines written to the file successfully")
f.close()
Output:
File names.txt:
Ramesh
Arjun
Senthil
Vignesh
4.2.4 Reading From a Text File
INFORMATION TECHNOLOGY [Mrs. Aesha K. Virani , Mrs. Hina S. Jayani] 7
Advanced Python Programming (UNIT-4) | 4321602
We can write a program to read the contents of a file. Before reading a file, we must make sure that the file
is opened in “r”, “r+”, “w+” or “a+” mode. There are three ways to read the contents of a file:
File2.txt
Python is the modern-day language. It makes things so simple.
It is the fastest-growing programming language Python has easy an syntax and user-friendly
interaction.
4.2.4.1The read() method
This method is used to read a specified number of bytes of data from a data file. The syntax of read()
method is:
fileobj.read(<count>)
Here, the count is the number of bytes to be read from the file starting from the beginning of the file. If the
count is not specified, then it may read the content of the file until the end.
Example:
#open the file.txt in read mode. causes error if no such file exists.
f = open("file2.txt","r")
#stores all the data of the file into the variable content
content = f.read(10)
# prints the type of the data stored in the file
print(type(content))
#prints the content of the file
print(content)
#closes the opened file
f.close()
O/P:
Python is
In the above code, we have read the content of file2.txt by using the read() function. We have passed count
value as ten which means it will read the first ten characters from the file. So, it only prints 'Python is'.
If we use the following line, then it will print all content of the file. For read the whole file contents, the
code is given below –
Example:
content = fileptr.read()
print(content)
O/P:
INFORMATION TECHNOLOGY [Mrs. Aesha K. Virani , Mrs. Hina S. Jayani] 8
Advanced Python Programming (UNIT-4) | 4321602
Python is the modern-day language. It makes things so simple.
It is the fastest-growing programming language Python has easy an syntax and user-friendly interaction.
4.2.4.2 The readline([n]) method
This method reads one complete line from a file where each line terminates with a newline ( \n)
character.
It can also be used to read a specified number (n) of bytes of data from a file but maximum up to the
newline character (\n).
Python facilitates to read the file line by line by using a function readline() method.
The readline() method reads the lines of the file from the beginning, i.e., if we use the readline()
method two times, then we can get the first two lines of the file.
Consider the following example which contains a function readline() that reads the first line of our
file "file2.txt" containing three lines.
Example:
#open the file.txt in read mode. causes error if no such file exists.
f = open("file2.txt","r");
#stores all the data of the file into the variable content
content = f.readline()
content1 = f.readline()
#prints the content of the file
print(content)
print(content1)
#closes the opened file
f.close()
O/P:
Python is the modern day language.
It makes things so simple.
4.2.4.3 The readlines() method
The method reads all the lines and returns the lines along with newline as a list of strings. The following
example uses readlines() to read data from the text file
Example:
#open the file.txt in read mode. causes error if no such file exists.
f = open("file2.txt","r");
#stores all the data of the file into the variable content
INFORMATION TECHNOLOGY [Mrs. Aesha K. Virani , Mrs. Hina S. Jayani] 9
Advanced Python Programming (UNIT-4) | 4321602
caontent = f.readlines()
#prints the content of the file
print(content)
#closes the opened file
f.close()
O/P:
['Python is the modern day language.\n', 'It makes things so simple.\n', 'Python has easy syntax and user-
friendly interaction.']
4.2.5 Creating a new file
The new file can be created by using one of the following access modes with the function open().
The open() function used so many parameters.
The syntax of it is given :file = open(path_to_file, mode)
x, a and w is the modes of open() function. The uses of these modes are given below -
x: it creates a new file with the specified name. It causes an error a file exists with the same name.
a: It creates a new file with the specified name if no such file exists. It appends the content to the file
if the file already exists with the specified name.
w: It creates a new file with the specified name if no such file exists. It overwrites the existing file.
Example:
#open the file.txt in read mode. causes error if no such file exists.
f = open("file2.txt","x")
if f:
print("File created successfully")
O/P:
<_io.TextIOWrapper name='file2.txt' mode='x' encoding='cp1252'>
File created successfully
4.3 Setting Offsets in a File
The functions that we have learnt till now are used to access the data sequentially from a file.
But if we want to access data in a random fashion, then Python gives us seek() and tell() functions to
do so.
4.3.1 The tell() method
This function returns an integer that specifies the current position of the file object in the file.
The position so specified is the byte position from the beginning of the file till the current position of
the file object.
INFORMATION TECHNOLOGY [Mrs. Aesha K. Virani , Mrs. Hina S. Jayani] 10
Advanced Python Programming (UNIT-4) | 4321602
The syntax of using tell() is : file_object.tell()
4.3.2 The seek() method
In real-world applications, sometimes we need to change the file pointer location externally since we
may need to read or write the content at various locations.
For this purpose, the Python provides us the seek() method which enables us to modify the file
pointer position externally.
That means, using seek() method we can easily change the cursor in the file, from where we want to
read or write a file.
The syntax of seek() is: <file-ptr>.seek(offset[, from)
The seek() method accepts two parameters:
offset: It refers to the new position of the file pointer within the file.
from: It indicates the reference position from where the bytes are to be moved. If it is set to 0, the beginning
of the file is used as the reference position. If it is set to 1, the current position of the file pointer is used as
the reference position. If it is set to 2, the end of the file pointer is used as the reference position.
Example:
# open the file file2.txt in read mode
f = open("file2.txt","r")
#initially the f is at 0
print("The filepointer is at byte :",f.tell())
#changing the file pointer location to 10.
f.seek(10);
#tell() returns the location of the f.
print("After reading, the filepointer is at:",f.tell())
O/P:
The filepointer is at byte : 0
After reading, the filepointer is at: 10
Explain object serialization. SUMMER-2022,SUMMER-2023
4.4 Object Serialization
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.
INFORMATION TECHNOLOGY [Mrs. Aesha K. Virani , Mrs. Hina S. Jayani] 11
Advanced Python Programming (UNIT-4) | 4321602
Serialization permits you to store complex records systems, consisting of lists, dictionaries,
and custom objects, to a document or transfer them over a community.
Python gives several integrated serialization modules, including pickle, JSON, and marshal.
4.4.1 Pickle
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.
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.
Define load() and dump() with syntax. WINTER-2022
4.4.1.1 The dump() method
This method is used to convert (pickling) Python objects for writing data in a binary file. The file in which
data are to be dumped, needs to be opened in binary write mode (wb).
Syntax of dump() is as follows: dump(data_object, fi le_object)
where data_object is the object that has to be dumped to the file with the file handle named file_object
Example:
import pickle
listvalues=[1,"Geetika",'F', 26]
f=open("mybinary.dat", "wb")
pickle.dump(listvalues,f)
f.close()
INFORMATION TECHNOLOGY [Mrs. Aesha K. Virani , Mrs. Hina S. Jayani] 12
Advanced Python Programming (UNIT-4) | 4321602
4.4.1.2 The load() method
This method is used to load (unpickling) data from a binary file. The file to be loaded is opened in binary
read (rb) mode.
Syntax of load() is as follows: Store_object = load(file_object)
Here, the pickled Python object is loaded from the file having a file handle named file_object and is stored
in a new file handle called store_object
Example:
import pickle
print("The data that were stored in fi le are: ")
f=open("mybinary.dat","rb")
objectvar=pickle.load(f)
f.close()
print(objectvar)
O/P:
The data that were stored in file are:
[1, 'Geetika', 'F', 26]
**********
“Do not give up, the beginning is always hardest!”
INFORMATION TECHNOLOGY [Mrs. Aesha K. Virani , Mrs. Hina S. Jayani] 13