Open In App

File Mode in Python

Last Updated : 04 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In Python, the file mode specifies the purpose and the operations that can be performed on a file when it is opened. When you open a file using the open() function, you can specify the file mode as the second argument.

Different File Mode in Python

Below are the different types of file modes in Python along with their description:

Mode

Description

‘r’Open text file for reading. Raises an I/O error if the file does not exist.
‘r+’Open the file for reading and writing. Raises an I/O error if the file does not exist.
‘w’Open the file for writing. Truncates the file if it already exists. Creates a new file if it does not exist.
‘w+’Open the file for reading and writing. Truncates the file if it already exists. Creates a new file if it does not exist.
‘a’Open the file for writing. The data being written will be inserted at the end of the file. Creates a new file if it does not exist.
‘a+’Open the file for reading and writing. The data being written will be inserted at the end of the file. Creates a new file if it does not exist.
‘rb’Open the file for reading in binary format. Raises an I/O error if the file does not exist.
‘rb+’Open the file for reading and writing in binary format. Raises an I/O error if the file does not exist.
‘wb’Open the file for writing in binary format. Truncates the file if it already exists. Creates a new file if it does not exist.
‘wb+’Open the file for reading and writing in binary format. Truncates the file if it already exists. Creates a new file if it does not exist.
‘ab’Open the file for appending in binary format. Inserts data at the end of the file. Creates a new file if it does not exist.
‘ab+’Open the file for reading and appending in binary format. Inserts data at the end of the file. Creates a new file if it does not exist.

File Mode in Python

Below are some of the file modes in Python:

Read Mode (‘r’) in Python

This mode allows you to open a file for reading only. If the file does not exist, it will raise a FileNotFoundError.

example.txt

Hello Geeks

Example:

In this example, a file named ‘example.txt’ is opened in read mode (‘r’), and its content is read and stored in the variable ‘content’ using a ‘with’ statement, ensuring proper resource management by automatically closing the file after use.

Python3
with open('example.txt', 'r') as file:
    content = file.read()

Output:

Hello Geeks

Write Mode (‘w’) in Python

This mode allows you to open a file for writing only. If the file already exists, it will truncate the file to zero length. If the file does not exist, it will create a new file.

example.txt

Hello, world!

Example:

In this example, a file named ‘example.txt’ is opened in write mode (‘w’), and the string ‘Hello, world!’ is written into the file.

Python3
with open('example.txt', 'w') as file:
    file.write('Hello, world!')

Output:

Hello, world!

Note – If you were to open the file “example.txt” after running this code, you would find that it contains the text “Hello, world!”.

Append Mode (‘a’) in Python

This mode allows you to open a file for appending new content. If the file already exists, the new content will be added to the end of the file. If the file does not exist, it will create a new file.

example.txt

Hello, World!
This is a new line

Example:

In this example, a file named ‘example.txt’ is opened in append mode (‘a’), and the string ‘\nThis is a new line.’ is written to the end of the file.

Python3
with open('example.txt', 'a') as file:
    file.write('\nThis is a new line.')

Output:

Hello, World!
This is a new line

The code will then write the string “\nThis is a new line.” to the file, appending it to the existing content or creating a new line if the file is empty.

Binary Mode (‘b’) in Python

This mode use in binary files, such as images, audio files etc. Its always used by combined with read (`’rb’`) or write (‘wb’) modes.

Example:

In this example, a file named ‘image.png’ is opened in binary read mode (‘rb’). The binary data is read from the file using the ‘read()’ method and stored in the variable ‘data’.

Python3
with open('image.png', 'rb') as file:
    data = file.read()
    # Process the binary data

Read and Write Mode (‘r+’) in Python

This mode allows you to open a file for both reading and writing. The file pointer will be positioned at the beginning of the file. If the file does not exist, it will raise a FileNotFoundError.

example.txt

This is a new line.
Initial content.
Python3
with open('example.txt', 'r+') as file:
    content = file.read()
    file.write('\nThis is a new line.')

Output:

If the initial contents of “example.txt” we are “Initial content.”, after running this code, the new content of the file would be:

This is a new line.
Initial content.

Write and Read Mode (‘w+’) in Python

This mode allows you to open a file for both reading and writing. If the file already exists, it will truncate the file to zero length. If the file does not exist, it will create a new file.

example.txt

Hello, world!

Example:

In this example, a file named ‘example.txt’ is opened in write and read mode (‘w+’).

Python3
with open('example.txt', 'w+') as file:
    file.write('Hello, world!')
    file.seek(0)
    content = file.read()

Output:

Therefore, the output of this code will be the string “Hello, world!”. Since the file was truncated and the pointer was moved to the beginning before reading, the contents of the file will be exactly what was written to it. So, content will contain the string “Hello, world!”.

Hello, world!


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads