Python File Handling
Till now, we were taking the input from the console and writing it back
to the console to interact with the user.
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.
The file-handling implementation is slightly lengthy or complicated in
the other programming language, but it is easier and shorter in Python.
In Python, files are treated in two modes as text or binary. The file may
be in the text or binary format, and each line of a file is ended with the
special character.
Hence, a file operation can be done in the following order.
o Open a file
o Read or write - Performing operation
o Close the file
Opening a file
Python provides an open() function that accepts two arguments, file
name and access mode in which the file is accessed. The function returns
a file object which can be used to perform various operations like
reading, writing, etc.
Syntax:
file object = open(<file-name>, <access-mode>, <buffering>)
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.
Let's look at the simple example to open a file named "file.txt" (stored in
the same directory) in read mode and printing its content on the console.
Example
1. #opens the file file.txt in read mode
2. fileptr = open("file.txt","r")
3.
4. if fileptr:
5. print("file is opened successfully")
Output:
<class '_io.TextIOWrapper'>
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 fileptr holds the file object and if the file is opened successfully, it
will execute the print statement.
SN Access Description
mode
1 r It opens the file to read-only mode. 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.
The close() method
Once all the operations are done on the file, we must close it through
our Python script using the close() method. Any unwritten information
gets destroyed once the close() method is called on a file object.
We can perform any operation on the file externally using the file system
which is the currently opened in Python; hence it is good practice to
close the file once all the operations are done.
The syntax to use the close() method is given below.
Syntax
fileobject.close()
Consider the following example
1. # opens the file file.txt in read mode
2. fileptr = open("file.txt","r")
3.
4. if fileptr:
5. print("file is opened successfully")
6.
7. #closes the opened file
8. fileptr.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.
1. try:
2. fileptr = open("file.txt")
3. # perform file operations
4. finally:
5. fileptr.close()
The with statement
The with statement was introduced in python 2.5. The with statement is
useful in the case of manipulating the files. It is used in the scenario
where a pair of statements is to be executed with a block of code in
between.
The syntax to open a file using with the statement is given below.
with open(<file name>, <access mode>) as <file-pointer>:
#statement suite
The advantage of using with statement is that it provides the guarantee
to close the file regardless of how the nested block exits.
It is always suggestible to use the with statement in the case of files
because, if the break, return, or exception occurs in the nested block of
code then it automatically closes the file, we don't need to write
the close() function. It doesn't let the file to corrupt.
Consider the following example.
Example
1. with open("file.txt",'r') as f:
2. content = f.read();
3. print(content)
Writing the file
To write some text to a file, we need to open the file using the open
method with one of the following access modes.
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.
Consider the following example.
Example
1. # open the file.txt in append mode. Create a new file if no such file e
xists.
2. fileptr = open("file2.txt", "w")
3.
4. # appending the content to the file
5. fileptr.write('''''Python is the modern day language. It makes things s
o simple.
6. It is the fastest-growing programing language''')
7.
8. # closing the opened the file
9. fileptr.close()
Output:
File2.txt
Python is the modern-day language. It makes things so simple. It is the
fastest growing programming language.
Snapshot of the file2.txt
We have opened the file in w mode. The file1.txt file doesn't exist, it
created a new file and we have written the content in the file using
the write() function.
Example 2
1. #open the file.txt in write mode.
2. fileptr = open("file2.txt","a")
3.
4. #overwriting the content of the file
5. fileptr.write(" Python has an easy syntax and user-
friendly interaction.")
6.
7. #closing the opened file
8. fileptr.close()
Output:
Python is the modern day language. It makes things so simple.
It is the fastest growing programing language Python has an easy syntax
and user-friendly interaction.
Snapshot of the file2.txt
We can see that the content of the file is modified. We have opened the
file in a mode and it appended the content in the existing file2.txt.
To read a file using the Python script, the Python provides
the read() method. The read() method reads a string from the file. It can
read the data in the text as well as a binary format.
The syntax of the read() method is given below.
Syntax:
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.
Consider the following example.
Example
1. #open the file.txt in read mode. causes error if no such file exists.
2. fileptr = open("file2.txt","r")
3. #stores all the data of the file into the variable content
4. content = fileptr.read(10)
5. # prints the type of the data stored in the file
6. print(type(content))
7. #prints the content of the file
8. print(content)
9. #closes the opened file
10. fileptr.close()
Output:
<class 'str'>
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.
If we use the following line, then it will print all content of the file.
content = fileptr.read()
print(content)
Output:
Python is the modern-day language. It makes things so simple.
It is the fastest-growing programing language Python has easy an syntax
and user-friendly interaction.
Read file through for loop
We can read the file using for loop. Consider the following example.
1. #open the file.txt in read mode. causes an error if no such file exists.
2. fileptr = open("file2.txt","r");
3. #running a for loop
4. for i in fileptr:
5. print(i) # i contains each line of the file
Output:
Python is the modern day language.
It makes things so simple.
Python has easy syntax and user-friendly interaction.
Read Lines of the file
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. Consider the following example.
Example 1: Reading lines using readline() function
1. #open the file.txt in read mode. causes error if no such file exists.
2. fileptr = open("file2.txt","r");
3. #stores all the data of the file into the variable content
4. content = fileptr.readline()
5. content1 = fileptr.readline()
6. #prints the content of the file
7. print(content)
8. print(content1)
9. #closes the opened file
10. fileptr.close()
Output:
Python is the modern day language.
It makes things so simple.
We called the readline() function two times that's why it read two lines
from the file.
Python provides also the readlines() method which is used for the
reading lines. It returns the list of the lines till the end of file(EOF) is
reached.
Example 2: Reading Lines Using readlines() function
1. #open the file.txt in read mode. causes error if no such file exists.
2. fileptr = open("file2.txt","r");
3.
4. #stores all the data of the file into the variable content
5. content = fileptr.readlines()
6.
7. #prints the content of the file
8. print(content)
9.
10. #closes the opened file
11. fileptr.close()
Output:
['Python is the modern day language.\n', 'It makes things so simple.\n',
'Python has easy syntax and user-friendly interaction.']
Creating a new file
The new file can be created by using one of the following access modes
with the function open().
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.
Consider the following example.
Example 1
1. #open the file.txt in read mode. causes error if no such file exists.
2. fileptr = open("file2.txt","x")
3. print(fileptr)
4. if fileptr:
5. print("File created successfully")
Output:
<_io.TextIOWrapper name='file2.txt' mode='x' encoding='cp1252'>
File created successfully
Creating the new directory
The mkdir() method is used to create the directories in the current
working directory. The syntax to create the new directory is given below.
Syntax:
mkdir(directory name)
Example 1
import os
#creating a new directory with the name new
os.mkdir("new")
The getcwd() method
This method returns the current working directory.
The syntax to use the getcwd() method is given below.
Syntax
os.getcwd()
Example
import os
os.getcwd()
Output:
'C:\\Users\\DEVANSH SHARMA'
Changing the current working directory
The chdir() method is used to change the current working directory to a
specified directory.
The syntax to use the chdir() method is given below.
Syntax:
chdir("new-directory")
Example
1. import os
2. # Changing current directory with the new directiory
3. os.chdir("C:\\Users\\DEVANSH SHARMA\\Documents")
4. #It will display the current working directory
5. os.getcwd()
Output:
'C:\\Users\\DEVANSH SHARMA\\Documents'
Deleting directory
The rmdir() method is used to delete the specified directory.
The syntax to use the rmdir() method is given below.
Syntax
os.rmdir(directory name)
Example 1
1. import os
2. #removing the new directory
3. os.rmdir("directory_name")
It will remove the specified directory.
Writing Python output to the files
In Python, there are the requirements to write the output of a Python
script to a file.
The check_call() method of module subprocess is used to execute a
Python script and write the output of that script to a file.
The following example contains two python scripts. The script file1.py
executes the script file.py and writes its output to the text
file output.txt.
Example
file.py
1. temperatures=[10,-20,-289,100]
2. def c_to_f(c):
3. if c< -273.15:
4. return "That temperature doesn't make sense!"
5. else:
6. f=c*9/5+32
7. return f
8. for t in temperatures:
9. print(c_to_f(t))
file.py
1. import subprocess
2.
3. with open("output.txt", "wb") as f:
4. subprocess.check_call(["python", "file.py"], stdout=f)
Python Exception
An exception can be defined as an unusual condition in a program
resulting in the interruption in the flow of the program.
Whenever an exception occurs, the program stops the execution, and
thus the further code is not executed. Therefore, an exception is the run-
time errors that are unable to handle to Python script. An exception is a
Python object that represents an error
Python provides a way to handle the exception so that the code can be
executed without any interruption. If we do not handle the exception,
the interpreter doesn't execute all the code that exists after the
exception.
Python has many built-in exceptions that enable our program to run
without interruption and give the output. These exceptions are given
below:
Common Exceptions
Python provides the number of built-in exceptions, but here we are
describing the common standard exceptions. A list of common
exceptions that can be thrown from a standard Python program is given
below.
1. ZeroDivisionError: Occurs when a number is divided by zero.
2. NameError: It occurs when a name is not found. It may be local or
global.
3. IndentationError: If incorrect indentation is given.
4. IOError: It occurs when Input Output operation fails.
5. EOFError: It occurs when the end of the file is reached, and yet
operations are being performed.
The problem without handling exceptions
As we have already discussed, the exception is an abnormal condition
that halts the execution of the program.
Suppose we have two variables a and b, which take the input from the
user and perform the division of these values. What if the user entered
the zero as the denominator? It will interrupt the program execution and
through a ZeroDivision exception. Let's see the following example.
Example
1. a = int(input("Enter a:"))
2. b = int(input("Enter b:"))
3. c = a/b
4. print("a/b = %d" %c)
5.
6. #other code:
7. print("Hi I am other part of the program")
Output:
Enter a:10
Enter b:0
Traceback (most recent call last):
File "exception-test.py", line 3, in <module>
c = a/b;
ZeroDivisionError: division by zero
The above program is syntactically correct, but it through the error
because of unusual input. That kind of programming may not be suitable
or recommended for the projects because these projects are required
uninterrupted execution. That's why an exception-handling plays an
essential role in handling these unexpected exceptions. We can handle
these exceptions in the following way.
Exception handling in python
The try-expect statement
If the Python program contains suspicious code that may throw the
exception, we must place that code in the try block. The try block must
be followed with the except statement, which contains a block of code
that will be executed if there is some exception in the try block.
Syntax
1. try:
2. #block of code
3.
4. except Exception1:
5. #block of code
6.
7. except Exception2:
8. #block of code
9.
10. #other code
Consider the following example.
Example 1
1. try:
2. a = int(input("Enter a:"))
3. b = int(input("Enter b:"))
4. c = a/b
5. except:
6. print("Can't divide with zero")
Output:
Enter a:10
Enter b:0
Can't divide with zero
We can also use the else statement with the try-except statement in
which, we can place the code which will be executed in the scenario if
no exception occurs in the try block.
The syntax to use the else statement with the try-except statement is
given below.
1. try:
2. #block of code
3.
4. except Exception1:
5. #block of code
6.
7. else:
8. #this code executes if no except block is executed
Consider the following program.
Example 2
1. try:
2. a = int(input("Enter a:"))
3. b = int(input("Enter b:"))
4. c = a/b
5. print("a/b = %d"%c)
6. # Using Exception with except statement. If we print(Exception) it
will return exception class
7. except Exception:
8. print("can't divide by zero")
9. print(Exception)
10. else:
11. print("Hi I am else block")
Output:
Enter a:10
Enter b:0
can't divide by zero
<class 'Exception'>
The except statement with no exception
Python provides the flexibility not to specify the name of exception
with the exception statement.
Consider the following example.
Example
1. try:
2. a = int(input("Enter a:"))
3. b = int(input("Enter b:"))
4. c = a/b;
5. print("a/b = %d"%c)
6. except:
7. print("can't divide by zero")
8. else:
9. print("Hi I am else block")
The except statement using with exception variable
We can use the exception variable with the except statement. It is used
by using the as keyword. this object will return the cause of the
exception. Consider the following example:
1. try:
2. a = int(input("Enter a:"))
3. b = int(input("Enter b:"))
4. c = a/b
5. print("a/b = %d"%c)
6. # Using exception object with the except statement
7. except Exception as e:
8. print("can't divide by zero")
9. print(e)
10. else:
11. print("Hi I am else block")
Output:
Enter a:10
Enter b:0
can't divide by zero
division by zero
Points to remember
1. Python facilitates us to not specify the exception with the except
statement.
2. We can declare multiple exceptions in the except statement since the
try block may contain the statements which throw the different type of
exceptions.
3. We can also specify an else block along with the try-except statement,
which will be executed if no exception is raised in the try block.
4. The statements that don't throw the exception should be placed inside
the else block.
Example
1. try:
2. #this will throw an exception if the file doesn't exist.
3. fileptr = open("file.txt","r")
4. except IOError:
5. print("File not found")
6. else:
7. print("The file opened successfully")
8. fileptr.close()
Output:
File not found
Declaring Multiple Exceptions
The Python allows us to declare the multiple exceptions with the
except clause. Declaring multiple exceptions is useful in the cases
where a try block throws multiple exceptions. The syntax is given
below.
Syntax
1. try:
2. #block of code
3.
4. except (<Exception 1>,<Exception 2>,<Exception 3>,...<Exception n>
)
5. #block of code
6.
7. else:
8. #block of code
Consider the following example.
1. try:
2. a=10/0;
3. except(ArithmeticError, IOError):
4. print("Arithmetic Exception")
5. else:
6. print("Successfully Done")
Output:
Arithmetic Exception
The try...finally block
Python provides the optional finally statement, which is used with
the try statement. It is executed no matter what exception occurs and
used to release the external resource. The finally block provides a
guarantee of the execution.
We can use the finally block with the try block in which we can pace the
necessary code, which must be executed before the try statement
throws an exception.
The syntax to use the finally block is given below.
Syntax
1. try:
2. # block of code
3. # this may throw an exception
4. finally:
5. # block of code
6. # this will always be executed
Multiple Exceptions
When a program encounters an exception during execution, it is
terminated if the exception is not handled. By handling multiple
exceptions, a program can respond to different exceptions without
terminating it.
In Python, try-except blocks can be used to catch and respond to one
or multiple exceptions. In cases where a process raises more than one
possible exception, they can all be handled using a single except clause.
There are several approaches for handling multiple exceptions in
Python, the most common of which are discussed below.
Using Same Code Block for Multiple Exceptions
With this approach, the same code block is executed if any of the listed
exceptions occurs. Here's an example:
try:
name = 'Bob'
name += 5
except (NameError, TypeError) as error:
print(error)
In the above example, the code in the except block will be executed if
any of the listed exceptions occurs. Running the above code raises
a TypeError, which is handled by the code, producing the following
output:
cannot concatenate 'str' and 'int' objects
Using Different Code Blocks for Multiple Exceptions
If some exceptions need to be handled differently, they can be placed
in their own except clause:
try:
name = 'Bob'
name += 5
except NameError as ne:
# Code to handle NameError
print(ne)
except TypeError as te:
# Code to handle TypeError
print(te)
In the above example, NameError and TypeError are two possible
exceptions in the code, which are handled differently in their own
except blocks.
Investigating Exceptions using If, Elif, Else Statements
Exceptions can also be checked using if-elif-else conditions, which can
be useful if the exception needs to be investigated further:
import errno
try:
f = open('/opt/tmp/myfile.txt')
except IOError as e:
if e.errno == errno.ENOENT:
print('File not found')
elif e.errno == errno.EACCES:
print('Permission denied')
else:
print e
Here, the variable e holds an instance of the raised IOError. The
additional status code of the exception is checked in
the if, elif and else blocks and the first match is executed:
File not found
Multiple Except Clauses Matching
There may be cases where a piece of code causes an exception that can
match multiple except clauses:
try:
f = open('/opt/tmp/myfile.txt')
except EnvironmentError:
print('Failed to open file')
except IOError:
print('File not found')
Since EnvironmentError is more general than IOError, it matches the
exception in the code above. The EnvironmentError except clause is
listed first, so the code within it gets executed, producing the following
output:
Failed to open file
Example
1. try:
2. fileptr = open("file2.txt","r")
3. try:
4. fileptr.write("Hi I am good")
5. finally:
6. fileptr.close()
7. print("file closed")
8. except:
9. print("Error")
Output:
file closed
Error
Raising exceptions
An exception can be raised forcefully by using the raise clause in Python.
It is useful in in that scenario where we need to raise an exception to
stop the execution of the program.
For example, there is a program that requires 2GB memory for
execution, and if the program tries to occupy 2GB of memory, then we
can raise an exception to stop the execution of the program.
The syntax to use the raise statement is given below.
Syntax
1. raise Exception_class,<value>
Points to remember
1. To raise an exception, the raise statement is used. The exception class
name follows it.
2. An exception can be provided with a value that can be given in the
parenthesis.
3. To access the value "as" keyword is used. "e" is used as a reference
variable which stores the value of the exception.
4. We can pass the value to an exception to specify the exception type.
Example
1. try:
2. age = int(input("Enter the age:"))
3. if(age<18):
4. raise ValueError
5. else:
6. print("the age is valid")
7. except ValueError:
8. print("The age is not valid")
Output:
Enter the age:17
The age is not valid
Example 2 Raise the exception with message
1. try:
2. num = int(input("Enter a positive integer: "))
3. if(num <= 0):
4. # we can pass the message in the raise statement
5. raise ValueError("That is a negative number!")
6. except ValueError as e:
7. print(e)
Output:
Enter a positive integer: -5
That is a negative number!
Example 3
1. try:
2. a = int(input("Enter a:"))
3. b = int(input("Enter b:"))
4. if b is 0:
5. raise ArithmeticError
6. else:
7. print("a/b = ",a/b)
8. except ArithmeticError:
9. print("The value of b can't be 0")
Output:
Enter a:10
Enter b:0
The value of b can't be 0
Custom Exception
The Python allows us to create our exceptions that can be raised from
the program and caught using the except clause. However, we suggest
you read this section after visiting the Python object and classes.
Consider the following example.
Example
1. class ErrorInCode(Exception):
2. def __init__(self, data):
3. self.data = data
4. def __str__(self):
5. return repr(self.data)
6.
7. try:
8. raise ErrorInCode(2000)
9. except ErrorInCode as ae:
10. print("Received error:", ae.data)
Output:
Received error: 2000