Python File Handling
Python File Handling
Opening a file
Python provides the open() function which 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.
Mode Description
Open a file for writing. Creates a new file if it does not exist or truncates the file if it
'w'
exists.
'x' Open a file for exclusive creation. If the file already exists, the operation fails.
Open for appending at the end of the file without truncating it. Creates a new file if it
'a'
does not exist.
1
file.closed
3
file.name
4
file.softspace
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
#opens the file file.txt in read mode
fileptr = open("file.txt","r")
if fileptr:
print("file is opened successfully")
Example
# opens the file file.txt in read mode
fileptr = open("file.txt","r")
if fileptr:
print("file is opened successfully")
#open the file.txt in read mode. causes error if no such file exists.
fileptr = open("file.txt","r");
#stores all the data of the file into the variable content
content = fileptr.read(9);
Consider the following example which contains a function readline() that reads the first line of our
file "file.txt" containing three lines.
Example
#open the file.txt in read mode. causes error if no such file exists.
fileptr = open("file.txt","r");
#stores all the data of the file into the variable content
content = fileptr.readline();
Example
#open the file.txt in read mode. causes an error if no such file exists.
fileptr = open("file.txt","r");
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.
w: It will overwrite the file if any file exists. The file pointer is at the beginning of the file.
Example 1
#open the file.txt in append mode. Creates a new file if no such file exists.
fileptr = open("file.txt","a");
Example 2
#open the file.txt in write mode.
fileptr = open("file.txt","w");
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 a
displayed on the console, and since the memory is volatile, it is impossible to recover the programmatically generated data again an
However, if we need to do so, we may store it onto the local file system which is volatile and can be accessed every time. Here, com
handling.
In this section of the tutorial, we will learn all about file handling in python including, creating a file, opening a file, closing a file, w
file, etc.
Opening a file
Python provides the open() function which accepts two arguments, file name and access mode in which the file is accessed. The fun
which can be used to perform various operations like reading, writing, etc.
The files can be accessed using various modes like read, write, or append. The following are the details about the access mode to op
SN Access Description
mode
1 R It opens the file to read-only. The file pointer exists at the beginning. The file is by default
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 with the same name. The file pointer exists at the beginning of
7 w+ It opens the file to write and read both. It is different from r+ in the sense that it overwrites the prev
whereas r+ doesn?t overwrite the previously written file. It creates a new file if no file exists. The fi
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 th
9 A It opens the file in the append mode. The file pointer exists at the end of the previously written file
new file if no file exists with the same name.
1 Ab It opens the file in the append mode in binary format. The pointer exists at the end of the previously
0 new file in binary format if no file exists with the same name.
1 a+ It opens a file to append and read both. The file pointer remains at the end of the file if a file exists.
1 file exists with the same name.
1 ab+ It opens a file to append and read both in binary format. The file pointer remains at the end of the fi
2
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 o
Example
#opens the file file.txt in read mode
fileptr = open("file.txt","r")
if fileptr:
print("file is opened successfully")
Output:
<class '_io.TextIOWrapper'>
We can perform any operation on the file externally in the file system is the file is opened in python, hence it is good practice to clo
operations are done.
1. fileobject.close()
Example
# opens the file file.txt in read mode
fileptr = open("file.txt","r")
if fileptr:
print("file is opened successfully")
1. 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.
fileptr = open("file.txt","r");
#stores all the data of the file into the variable content
content = fileptr.read(9);
Output:
<class 'str'>
Hi, I am
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 "file.txt" containing
three lines.
Example
#open the file.txt in read mode. causes error if no such file exists.
fileptr = open("file.txt","r");
#stores all the data of the file into the variable content
content = fileptr.readline();
Output:
<class 'str'>
Example
#open the file.txt in read mode. causes an error if no such file exists.
fileptr = open("file.txt","r");
Output:
an example to read a
file in python.
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.
w: It will overwrite the file if any file exists. The file pointer is at the beginning of the file.
Consider the following example.
Example 1
#open the file.txt in append mode. Creates a new file if no such file exists.
fileptr = open("file.txt","a");
File.txt:
Now, we can check that all the previously written content of the file is overwritten with the new text we have passed.
File.txt:
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.
fileptr = open("file2.txt","x");
print(fileptr)
if fileptr:
print("File created successfully");
Example
with open("file.txt",'r') as f:
content = f.read();
print(content)
Example
# open the file file2.txt in read mode
fileptr = open("file2.txt","r")
#initially the filepointer is at 0
print("The filepointer is at byte :",fileptr.tell())
#reading the content of the file
content = fileptr.read();
#after the read operation file pointer modifies. tell() returns the location of the fileptr.
print("After reading, the filepointer is at:",fileptr.tell())
1. rename(?current-name?, ?new-name?)
Example
import os;
1. remove(?file-name?)
Example
import os;