Unit 5 - 1. File Handling in Python
Unit 5 - 1. File Handling in Python
Unit 5 - 1. File Handling in Python
Table of Contents
• What are files • Reading file contents
• read() method to read files
• What is file handling • read() method example with size defined
• readline() method to read a single line
• File operations
• readlines() method to read all lines
• File access modes • For loop to read lines
• List of all file access modes • Reading and Modifying file pointer position
• tell() method to get file pointer position
• Create a new file • seek() method to move file pointer position
• Closing a file is necessary as it would save the changes made on our file from
the current session.
File access modes
• We can specify the access mode while opening a file.
• There are 3 types of file access modes
• Read (r) – This mode is used to read file contents.
• Write (w) – This mode is used to write contents to file. Old contents are overwritten.
• Append (a) – This mode is used to write contents at the end of existing contents.
• We can also specify if we want to open the file in text mode or binary mode.
• The default is reading in text mode. In this mode, we get strings when
reading from the file.
• On the other hand, binary mode returns bytes and this is the mode to be
used when dealing with non-text files like images or executable files.
List of all file access modes
It opens the file to read-only mode. The file pointer exists at the It opens the file to write and read both. It is different from r+ in the sense that
r beginning. The file is by default open in this mode if no access w+ it overwrites the previous file. It creates a new file if no file exists. The file
mode is passed. pointer exists at the beginning of the file.
rb It opens the file to read-only in binary format. The file pointer wb+ It opens the file to write and read both in binary format. The file pointer exists
at the beginning of the file.
exists at the beginning of the file.
It opens the file in the append mode. The file pointer exists at the end of the
It opens the file to read and write both. The file pointer exists at a previously written file if exists any. It creates a new file if no file exists with
r+ the beginning of the file. the same name.
It opens the file in the append mode in binary format. The pointer exists at
rb+ It opens the file to read and write both in binary format. The file
pointer exists at the beginning of the file. ab the end of the previously written file. It creates a new file in binary format if
no file exists with the same name.
It opens the file to write only. It overwrites the file if previously It opens a file to append and read both. The file pointer remains at the end of
w exists or creates a new one if no file exists with the same name. a+ the file if a file exists. It creates a new file if no file exists with the same name.
The file pointer exists at the beginning of the file.
It opens the file to write only in binary format. It overwrites the It opens a file to append and read both in binary format. The file pointer
ab+ remains at the end of the file.
wb file if it exists previously or creates a new one if no file exists.
The file pointer exists at the beginning of the file.
Create a new file
• All the examples in this section use mytext.txt file. Run the below code to
create the mentioned file in your current working directory.
# opens the file in write & read mode, if file is not found a new file
will be created
fobj = open('myfile.txt1', 'w+')
open() function example
# opens the file in append mode, if file is not found a new file will be created
fobj = open('myfile.txt1', 'a')
# opens the file in append & read mode, if file is not found a new file will be
created
fobj = open('myfile.txt1', 'a+')
# opens the file in read mode in binary format, if file is not found an error will
occur
fobj = open('myfile.txt1', 'rb')
# opens the file in write mode in binary format, if file is not found a new file
will be created
fobj = open('myfile.txt1', 'wb)
close() method to close a file
• After opening and performing operations on the file, we need to
properly close the file.
• Closing a file will free up the resources that were tied with the file. It
is done using the close() method available in Python.
• Python has a garbage collector to clean up unreferenced objects but
we must not rely on it to close the file.
• Syntax: fileobject.close()
close() method example
# open a file in read mode
fobj = open('abc.txt', 'r')
# read the contents of file
contents = fobj.read()
# close the file connection
fobj.close()
close() method with exception handling
• A safer way is to use a try...finally block, This way, we are guaranteeing that the file is
properly closed even if an exception is raised that causes program flow to stop.
try:
# open a file in read mode
fobj = open('abc.txt', 'r')
# reading the contents
contents = fobj.read()
finally:
# close the file, whether an error/exception occurs
fobj.close()
with statement for closing file
• The with statement in Python is used for resource management and
exception handling.
• It simplifies the management of common resources like file streams.
• The advantage of using with statement is that it provides the guarantee to
close the file regardless of any exception.
• It automatically closes the file, we don't need to call the close() method. It
doesn't let the file to corrupt.
• Syntax:
with open(<file name>, <access mode>) as <file-object>:
#statements …
With statement example
• In this example we have not used the statement fobj.close()
anywhere.
• The file close operation is automatically executed by with statement.
with open('myfile.txt', 'r') as fobj:
# reading the contents
contents = fobj.read()
print(contents)
Reading file contents
• To read contents of file, Python provides various methods defined in
the file object. The are:
1. read()
2. readline()
3. readlines()
read() method to read files
• To read contents of a file, the Python provides read() method.
• It can read the data in the text as well as a binary format.
• We can use the size parameter to read upto the given size in bytes.
• If the size parameter is not specified, it reads and returns contents up
to the end of the file EOF.
• Syntax:
fileobject.read([size])
read() method example
# open the file in read mode
fobj = open('myfile.txt', 'r')
contents = fobj.read()
print(contents)
fobj.close()
Output:
hello how are you ??
I am fine !!
How are you doing ?
read() method example with size defined
# open the file in read mode
fobj = open('myfile.txt', 'r')
fobj.close()
Output:
hello how are you ??
I am
readline() method to read a single line
• 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
• For example, If we use the readline() method two times, then we can
get the first two lines of the file.
• Syntax:
fileobject.readline()
readline() method example
Here we have disabled the '\n' new line of print() function since the lines in the file
already have \n character appended at their end.
fobj.close()
readlines() method to read all lines
• To read all the lines of a file at once, readlines() method is used.
• The readlines() method reads all the lines of the file from the
beginning till the end of file (EOF) is reached
• The readlines() method returns a list object that contains all the lines
of the opened file in the form of list elements.
• Syntax:
fileobject.readlines()
readlines() method example
# open the file in read mode
fobj = open('myfile.txt', 'r')
lines = fobj.readlines()
print('List =', lines)
fobj.close()
For loop to read lines
• We can use for loop to directly iterate over the file object itself,
• The loop will return one line at each iteration.
• It will return all the lines until EOF is reached.
# read operation
contents = fobj.read()
fobj.close()
Output:
Initial Position: 0
Final position: 55
seek() method to move file pointer position
• seek() function is used to change the position of the File Handle to a
given specific position.
• File handle is like a cursor, which defines from where the data has to
be read or written in the file.
• seek() method accepts 2 parameters.
• Syntax:
fileobject.seek(offset, from_where)
Where, offset: Number of positions to move forward, can be positive(start) or negative(from end)
from_what: It defines point of reference. It has three values 0 (default), 1, 2
seek() method example
fobj = open('myfile.txt', 'r')
print('\n')
# skip 1st and 2nd line, move the pointer to 36th character
fobj.seek(36)
contents = fobj.read()
print(contents, end='')
fobj.close()
Write contents to file
• In order to write into a file in Python, we need to open it in write w,
append a or exclusive creation x mode.
• We need to be careful with the w mode, as it will overwrite into the
file if it already exists. Due to this, all the previous data are erased.
• To write the text or binary data into a file in Python following
methods are use:
1. write() method
2. writelines() method
write() method using write access mode
• write() method is used to write a single or multiple lines at once in a file. In
write access mode the old contents of the file are overwritten. The write()
function does not add a newline character at the end of each line.
# if file is not found a new file will be created
fobj = open('abc.txt','w')
# write 2 lines to the file using write() method
fobj.write("Delhi is the capital of India\n")
fobj.write("Tokyo is the capital of Japan")
# close the file object
fobj.close()
write() method using append as access mode
• In apped mode the write() method will write (append) the new contents after
the end of the existing contents in the file.
fobj.write('\nCapital of countries')
truncate() method to clear contents of file
• truncate() method is used to delete contents (data) from the file.
• It also take size as a parameter to delete certain amount of data.
• truncate() works with write access mode.
• Syntax: fileobject.truncate(size)
fobj = open('abc.txt','w')
fobj.truncate()
fobj.close()