Python - File Handling
Python - File Handling
Where, filename is the name of the file to open and mode is the mode in which the file is opened (e.g., 'r'
for reading, 'w' for writing, 'a' for appending).
r
1 Opens a file for reading only. The file pointer is placed at the beginning of the file. This is the
default mode.
rb
2 Opens a file for reading only in binary format. The file pointer is placed at the beginning of
the file. This is the default mode.
r+
3
Opens a file for both reading and writing. The file pointer placed at the beginning of the file.
rb+
4 Opens a file for both reading and writing in binary format. The file pointer placed at the
beginning of the file.
w
5 Opens a file for writing only. Overwrites the file if the file exists. If the file does not exist,
creates a new file for writing.
b
6
Opens the file in binary mode
t
7
Opens the file in text mode (default)
+
8
open file for updating (reading and writing)
wb
9 Opens a file for writing only in binary format. Overwrites the file if the file exists. If the file
does not exist, creates a new file for writing.
w+
10 Opens a file for both writing and reading. Overwrites the existing file if the file exists. If the
file does not exist, creates a new file for reading and writing.
wb+
11 Opens a file for both writing and reading in binary format. Overwrites the existing file if the
file exists. If the file does not exist, creates a new file for reading and writing.
a
12 Opens a file for appending. The file pointer is at the end of the file if the file exists. That is,
the file is in the append mode. If the file does not exist, it creates a new file for writing.
ab
Opens a file for appending in binary format. The file pointer is at the end of the file if the file
13
exists. That is, the file is in the append mode. If the file does not exist, it creates a new file
for writing.
a+
Opens a file for both appending and reading. The file pointer is at the end of the file if the file
14
exists. The file opens in the append mode. If the file does not exist, it creates a new file for
reading and writing.
ab+
Opens a file for both appending and reading in binary format. The file pointer is at the end of
15
the file if the file exists. The file opens in the append mode. If the file does not exist, it
creates a new file for reading and writing.
x
16
open for exclusive creation, failing if the file already exists
Once a file is opened and you have one file object, you can get various information related to that file.
Example 1
In the following example, we are opening a file in different modes −
Example 2
In here, we are opening a file named "foo.txt" in binary write mode ("wb"), printing its name, whether it's
closed, and its opening mode, and then closing the file −
Open Compiler
# Open a file
fo = open("foo.txt", "wb")
print ("Name of the file: ", fo.name)
print ("Closed or not: ", fo.closed)
print ("Opening mode: ", fo.mode)
fo.close()
Learn Python in-depth with real-world projects through our Python certification course. Enroll and
become a certified expert to boost your career.
To read a file, you need to open it in read mode. The default mode for the open() function is
read mode ('r'), but it's good practice to specify it explicitly.
In the following example, we are using the read() method to read the whole file into a single string −
Hello!!!
Welcome to TutorialsPoint!!!
In here, we are using the readline() method to read one line at a time, making it memory efficient for
reading large files line by line −
Hello!!!
Welcome to TutorialsPoint!!!
Hello!!!
Welcome to TutorialsPoint!!!
To write data to a file, use the write() or writelines() methods. When opening a file in write mode ('w'),
the file's existing content is erased.
In this example, we are using the write() method to write the string passed to it to the file. If the file is
opened in 'w' mode, it will overwrite any existing content. If the file is opened in 'a' mode, it will append
the string to the end of the file −
Open Compiler
In here, we are using the writelines() method to take a list of strings and writes each string to the file. It
is useful for writing multiple lines at once −
Open Compiler
Example
In this example, we open the file for writing, write data to the file, and then close the file using the
close() method −
Open Compiler
In this example, the file is automatically closed at the end of the with block, so there is no need to call
close() method explicitly −
Open Compiler
In Python, we use a try-finally block to handle exceptions when closing a file. The "finally" block ensures
that the file is closed regardless of whether an error occurs in the try block −
Open Compiler
try:
file = open("example.txt", "w")
file.write("This is an example with exception handling.")
finally:
file.close()
print ("File closed successfully!!")