Unit 5 - 1. File Handling in Python

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 36

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

• open() function to open a file • Write contents to file


• write() method using write access mode
• close() method to close a file • write() method using append as access mode
• writelines() to write multiple lines
• close() method with exception handling • with statement to write file contents
• with statement for closing file • truncate() method to clear contents of file
What are files
• Files are named locations on disk to store related information. They are used to
permanently store data in a non-volatile memory (e.g. hard disk).
• Since Random Access Memory (RAM) is volatile (which loses its data when the
computer is turned off), we use files for future use of the data by permanently
storing them.
• Data is stored in files in two ways:
1. Text Format- In text format data is stored as a line of character with each line
terminated by a new line character (\n). Text files are in human readable form.
2. Binary Format– In binary format, data is stored on the disk same way as it is
represented in the computer memory. Binary files are not in human readable
form they and can be created and read by a specific program written for them.
What is file handling
• File handling, in any computer language, means working with files.
• Specifically, creating, deleting, opening, closing, reading the contents
of, and writing new contents to files in the computer.
• Example: Every time you work with a Word file called xyz.docx on
your computer’s hard disk, a program (MS Word), opens the file,
reads the old contents, modifies the contents, writes the new
contents back, and closes it. These operations, collectively, are called
file handling operations.
• Thus file handling can be stated as a process by which a software
handles its Input/Output operations with binary/text files.
File operations
• 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:
1. Open a file
2. Read or write (perform operation)
3. Close the file

• 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.

​ # if file is not found a new file will be created in write mode


​ fobj = open('mytext.txt','w')

# write the 3 lines to the file using write() method
​ fobj.write("Hello how are you ??\n")
​ fobj.write("I am fine !!\n")
​ fobj.write("How are you doing ?")

# close the file object
​ fobj.close()
open() function to open a file
• Before performing any operation on the file like read or write, first we
have to open that file.
• At the time of opening, we have to specify the access mode, which
represents the purpose of the opening file.
• Python has a built-in open() function to open a file.
• This function returns a file object, also called a handle, as it is used to
read or modify the file accordingly through inbuilt methods.
• Syntax:
​ fileobject = open(file_name, access_mode)
open() function example
​ # opens the file in read mode, if file is not found an error will occur
​ fobj = open('myfile.txt1', 'r')

# opens the file in read & write mode, if file is not found an error occur
​ fobj = open('myfile.txt1', 'r+')

# opens the file in write mode, if file is not found a new file will be
created
​ fobj = open('myfile.txt1', 'w')

​ # 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')

# read(size) will return starting 25 characters


# including whitespaces and linebreak
contents = fobj.read(25)
print(contents)

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.

# open the file in read mode


fobj = open('myfile.txt', 'r')

# reading the content line by line


print(fobj.readline(), end='')
print(fobj.readline(), 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)

# use for loop to print lines


for line in lines:
    print(line, end='')

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.

​ # open the file in read mode


​ fobj = open('myfile.txt', 'r')

for line in fobj:
​     print(line, end='')

fobj.close()
Reading and Modifying file pointer position
• Sometimes we need to change the file pointer position since we may
need to read or write the content at various locations within the file.
• There are 2 methods defined in Python to handle file pointer position:
1. tell() method is used to get the byte number at which the file
pointer currently exists.
2. seek() method enables us to modify the file pointer position at the
given location.
tell() method to get file pointer position
• File handle (or pointer) is like a cursor, which defines from where the data
has to be read or written in the file.
• Sometimes it becomes important for us to know the position of the File
Handle.
• tell() method can be used to get the position of File Handle.
• tell() method returns current position of file object as an integer.
• Initially file pointer points to the beginning of the file(if not opened in
append mode). So, the initial value of tell() is zero.
• Syntax:
fileobject.tell()
tell() method example
fobj = open('myfile.txt', 'r')

# initial position of pointer is 0


print(fobj.tell())

# read operation
contents = fobj.read()

# after a full read operation, cursor will be at the end of file


print(fobj.tell())

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')

# skip 1st line, move the pointer to 22nd character


fobj.seek(22)
contents = fobj.read()
print(contents, end='')

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.

​ # apped mode will add new contents at the end of file


​ fobj = open('abc.txt','a')

# write 2 lines to the existing file using write() method
​ fobj.write("Sydney is the capital of Australia\n")
​ fobj.write("Washington is the capital of America")

# close the file object
​ fobj.close()
writelines() to write multiple lines
• The writelines() method writes the items of a list to the file. This function
does not add a newline character(\n) to the end of the string.
​ # if file is not found a new file will be created
​ fobj = open('abc.txt','a')

# list object
​ continents = ['\n', 'Europe\n', 'Antarctica\n', 'Africa\n']
​ fobj.writelines(continents)

# close the file object
​ fobj.close()
with statement to write file contents
with open('abc.txt','a') as fobj:

    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()

You might also like