When working with files in Python, the file mode tells Python what kind of operations (read, write, etc.) you want to perform on the file. You specify the mode as the second argument to the open() function.
Different File Mode in Python
Below are the different types of file modes in Python along with their description:
Mode | Description |
|---|
| ‘r’ | Read-only. Raises I/O error if file doesn't exist. |
| ‘r+’ | Read and write. Raises I/O error if the file does not exist. |
| ‘w’ | Write-only. Overwrites file if it exists, else creates a new one. |
| ‘w+’ | Read and write. Overwrites file or creates new one. |
| ‘a’ | Append-only. Adds data to end. Creates file if it doesn't exist. |
| ‘a+’ | Read and append. Pointer at end. Creates file if it doesn't exist. |
| ‘rb’ | Read in binary mode. File must exist. |
| ‘rb+’ | Read and write in binary mode. File must exist. |
| ‘wb’ | Write in binary. Overwrites or creates new. |
| ‘wb+’ | Read and write in binary. Overwrites or creates new. |
| ‘ab’ | Append in binary. Creates file if not exist. |
| ‘ab+’ | Read and append in binary. Creates file if it does not exist. |
Examples of Common Modes
Let's say we have a file named example.txt with the content:
Hello Geeks
We'll use this file for the examples below.
1. Read Mode ('r')
This mode allows you to open a file for reading only. If the file does not exist, it will raise a FileNotFoundError.
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.
Python
with open('example.txt', 'r') as file:
content = file.read()
Output:
Hello Geeks
2. Write Mode ('w')
Opens the file for writing only. If the file exists, its content is deleted. If not, a new file is created.
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.
Python
with open('example.txt', 'w') as file:
file.write('Hello, world!')
Output (file content after writing):
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!" as the previous content "Hello Geeks" will be deleted.
3. Append Mode ('a')
Opens the file to add content at the end without deleting existing data. If the file doesn’t exist, it creates a new one.
Example: In this example, a file named 'example.txt' is opened in append mode ('a'), and the string '\n This is a new line.' is written to the end of the file.
Python
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.
4. Binary Mode ('b')
Used for non-text files like images or audio. Always combined with 'r', 'w', or 'a
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'.
Python
with open('image.png', 'rb') as file:
data = file.read()
# Process the binary data
5. Read and Write Mode ('r+')
Opens the file for both reading and writing. Starts at the beginning of the file. Raises FileNotFoundError if the file doesn’t exist.
Python
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" were:
Hello, World!
This is a new line
After running the code, the new content of the file would be:
This is a new line
Hello, World!
This is a new line
6. Write and Read Mode ('w+')
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: In this example, a file named 'example.txt' is opened in write and read mode ('w+').
Python
with open('example.txt', 'w+') as file:
file.write('Hello, world!')
file.seek(0)
content = file.read()
Output:
Hello, world!
Explanation: the output of this code is "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!".
What is the purpose of the 'rb' mode in file opening?
Explanation:
The 'rb' mode is used for reading binary data from a file.
What is the purpose of the 'a' mode in file opening?
Explanation:
The 'a' mode is used for appending data to an existing file
How can you write data to a file without overwriting its existing content in Python?
-
Open the file with mode 'w' and call write()
-
Open the file with mode 'a' and call write()
-
-
Explanation:
To append data, open the file in 'a' mode, then use write() to add data.
What does the 'a+b' mode stand for in file opening?
Explanation:
The 'a+b' mode is used for both appending and reading binary data.
What is the purpose of the 'a+U' mode in file opening?
-
Append and Unicode support
-
Access and Universal newline support
-
-
Explanation:
The 'a+U' mode is used for both appending and reading with universal newline support.
Quiz Completed Successfully
Your Score : 2/5
Accuracy : 0%
Login to View Explanation
1/5
1/5
< Previous
Next >
Explore
Python Fundamentals
Python Data Structures
Advanced Python
Data Science with Python
Web Development with Python
Python Practice