CHAPTER 2: FILE HANDLING
Chapter 2: File Handling in Python
We store python programs written in script mode with .py extension. Each 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.
Types of file
There are mainly two types of data file.
1. Text file
2. Binary file
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, .doc etc are some examples of text files.
When we open a text file using a text editor.
In ASCII 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 of 65 will be displayed by a text editor as the letter ‘A’
since the number 65 in ASCII character set represent ‘A’.
Each line of a text file is terminated by a special character, called the End of Line
(EOL). The default EOL character in oython is the newline (\n).
Contents in a text files are usually separated by whitespaces, but comma(,) and
tab(\t) are also commonly used to separate values in a text file.
Prepared By: Sowmya Rani GS, Lecturer, Dept. of Computer Science, Chitradurga
CHAPTER 2: FILE HANDLING
Binary File:
Binary file are also stored in terms of bytes (0’s and 1’s), 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, compresses versions of
other files, executable files, etc.
These files are not human readable.
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.
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.
Differentiate between text file and binary file
Text File Binary File
1. A text file consists of human 1. A binary file is made up of non-
readable characters, which can be human readable characters
opened by any text editor. and symbols, which require
specific
programs to access its contents.
2. A text file is a file that stores 2. A binary file is a file that stores the
information in the form of a stream information in the form of a stream
of ASCII or Unicode characters. of bytes.
3. In text files, each line of text is 3. In a binary file, there is no delimiter
terminated with a special character for a line and no character
known as EOL (End of Line) translations occur here.
characters.
4. Files with extensions like .txt, .py, 4. Files with extensions like .jpg, .pdf
.doc, .csv etc are some examples of etc are some examples of binary
Prepared By: Sowmya Rani GS, Lecturer, Dept. of Computer Science, Chitradurga
CHAPTER 2: FILE HANDLING
text files. files.
Steps in Data File Handling
1. Opening a File
2. Performing Read / Write operation
3. Closing a File
1. Opening a file:
To open a file in python, we use the open( ) function.
The syntax of open( ) is as follows:
File_object = open(file_name, access_mode)
This function returns a file object called file handle which is stored in the
variable file_object.
The file_object has certain attributes that tells us basic information about
the file, such as
o <file.closed> returns true if the file is closed and false otherwise.
o <file.mode> returns the access mode in which the file was opened.
o <file.name> returns the name of the file.
The access mode is the operation for which the file has to be opened like
for reading and writing, for appending at the end of an existing file.
File access mode /File Open Modes
Prepared By: Sowmya Rani GS, Lecturer, Dept. of Computer Science, Chitradurga
CHAPTER 2: FILE HANDLING
2. Closing a File:
After all operations on a file, it is necessary to close the file.
Python provides a close( ) method to close the file.
The syntax of close( ) is: file_object.close( )
3. 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.
With open(“Myfile.txt”, “r+”) as myObject:
Content = myObject.read( )
4. Writing to a Text File
For writing to a file, 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.
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.
Prepared By: Sowmya Rani GS, Lecturer, Dept. of Computer Science, Chitradurga
CHAPTER 2: FILE HANDLING
The following methods to write data in the file:
1. write ( )- for writing a single string
2. writelines( ) – for writing a sequence of stings
write( ) Method
write( ) method takes a string as an argument and writes it to the text file.
It returns the number of characters being written.
We need to add a newline character (\n) at the end of every sentence to mark
the end of line.
Consider the following code:
The writelines( ) method
This method is used to write multiple strings to a file. We need to pass an object
like list, tuple, etc containing strings to the writelines( ) method.
The following code explain the use of writelines( )
5. Reading from a text file
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:
1. The read( ) Method:
Prepared By: Sowmya Rani GS, Lecturer, Dept. of Computer Science, Chitradurga
CHAPTER 2: FILE HANDLING
This method is used to read a specified number of bytes of data from a data
file.
The syntax of read( ) method
is: File_obejct.read( n)
Consider the following set of statements to understand the usage of read( ) method:
myobject = open (“myfile.txt”, ‘r’)
myobject.read(10) ‘Hello
ever’ myobject.close( )
If no argument or a negative number is specified in read( ), the entire filw
content is read. For example: output: Hello everyone
myobject = open (“myfile.txt”, ‘r’) Writing multiline strings
print(myobject.read( )) This is the third line
myobejct.close( )
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).
In the following example, the second statement reads the first ten characters of
the first line of the text file and displays them on the screen.
>>> myobejct = open (“myfile.txt”,’r’)
>>> myobject.readline(10)
‘Hello ever’
>>> myobject.close( )
If no argument or negative number specified, it reads a complete line and returns
string.
>>> myobejct = open (“myfile.txt”,’r’)
>>> myobject.readline( )
‘Hello everyone\n’
3. The readlines( ) method
Prepared By: Sowmya Rani GS, Lecturer, Dept. of Computer Science, Chitradurga
CHAPTER 2: FILE HANDLING
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 myfile.txt
myobejct = open (“myfile.txt”,’r’)
print( myobject.readlines( ))
[‘Hello everyone\n’, ‘writing multiline strings\n’, ‘This is the third line’]
myobject.close( )
In case we want to display each word of a line separately as an element of a list,
then we can use split( ) function. The following code demonstrate the use of
split( ) function.
myobject=open(“myfiles.txt”, ‘r’)
d=myobject.readlines( )
for line in d:
words=line.split( )
print(words)
[‘Hello’,’everyone’]
[‘Writing’, ‘multiline’,’strings’]
[‘This’,’is’,’the’,’third’,line’]
However, if the splitlines( ) is used instead of split( ), then each line is returned
as element of a list, as shown in the output below:
Setting Offsets in a File
If we want to access data in a random fashion, then python gives us seek( ) and tell( )
functions to do so.
Prepared By: Sowmya Rani GS, Lecturer, Dept. of Computer Science, Chitradurga
CHAPTER 2: FILE HANDLING
The syntax of using tell( ) is: file_object.tell( )
The syntax of seek( ) is: file_object.seek(offset[,reference_point])
Creating and traversing a text file
1. Creating a file and writing data:
Create a text file, we use the open( ) method and provide the filename and the
mode.
If the file already exists with the same name, the open( ) function will behave
differently depending on the mode(write or append) used.
If it is in write mode (w), then all the existing contents of file will be lost, and an empty
file will be created with the same name.
If the file is created in append mode(a), then the new data will be written after
the existing data.
Prepared By: Sowmya Rani GS, Lecturer, Dept. of Computer Science, Chitradurga
CHAPTER 2: FILE HANDLING
In both the cases, if the file does not exist, then a new empty file will be created.
2. Traversing a file and displaying data
To read and display data that is stored in a text file. The file will be opened in read
mode and reading will begin from the beginning if the file.
The Pickle Module
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.
Prepared By: Sowmya Rani GS, Lecturer, Dept. of Computer Science, Chitradurga
CHAPTER 2: FILE HANDLING
Syntax of dump ( ) is as follows:
dump(data_obejct, file_object)
Store_obejct = load(file_object)
Prepared By: Sowmya Rani GS, Lecturer, Dept. of Computer Science, Chitradurga