In Post #145, we learned that the Pythonic way to open a file is with the with open()
statement. We used the syntax with open("my_file.txt", "w") as file:
. That second argument, "w"
, is the file mode.
The file mode is a crucial single-character string that tells Python what you intend to do with the file. Do you want to read its contents, completely overwrite it, or just add something to the end? Choosing the right mode is essential for preventing errors and accidental data loss.
In this post, we’ll explore the four most common and essential file modes: 'r'
, 'w'
, 'a'
, and 'x'
.
‘r’ – Read Mode
The 'r'
mode is for reading from a file. This is the most common mode and is also the default. If you don’t specify a mode when calling open()
, Python will assume you mean 'r'
.
with open("my_data.txt", "r") as f:
is the same as with open("my_data.txt") as f:
.
- Key Behavior: You can only read data from a file opened in this mode. If you try to write to it, your program will raise an error.
- Crucial Rule: This mode requires the file to already exist. If the file you are trying to open does not exist, Python will raise a
FileNotFoundError
.
‘w’ – Write Mode
The 'w'
mode is for writing to a file. It gives you an empty canvas to write new content.
This mode has two important and very different behaviors depending on whether the file exists:
- If the file does not exist: Python will create a new, empty file for you.
- If the file already exists (DANGER): Its entire contents will be deleted the moment the file is opened, before you even write anything new. Be very careful with write mode, as you can easily and irreversibly overwrite important data by accident.
‘a’ – Append Mode
The 'a'
mode is for appending to a file. It’s a safer way to write data to a file without losing what’s already there.
- Key Behavior: This mode opens a file for writing, but instead of starting the cursor at the beginning (and erasing everything), the cursor is placed at the very end of the file. Any new data you write is added to the end of the existing content.
- File Existence:
- If the file does not exist: Python will create a new, empty file for you (just like
'w'
mode). - If the file already exists: Its current contents are preserved.
- If the file does not exist: Python will create a new, empty file for you (just like
This is the perfect mode for tasks like adding new entries to a log file or a journal.
‘x’ – Exclusive Creation Mode
The 'x'
mode is for exclusive creation. It’s a special, safer version of write mode, designed to prevent accidental overwrites.
- Key Behavior: This mode will create a new file and open it for writing, but only if that file does not already exist.
- Crucial Rule: If the file you are trying to create already exists, Python will not open it. Instead, it will raise a
FileExistsError
.
Use 'x'
mode when your program needs to create a new file and you want to be absolutely sure you are not overwriting a file that was already there.
Summary
Mode | Meaning | What it Does | If File Exists | If File Doesn’t Exist |
'r' | Read | Opens for reading. | Reads from start. | FileNotFoundError |
'w' | Write | Opens for writing. | Erases all content. | Creates new file. |
'a' | Append | Opens for writing. | Appends to end. | Creates new file. |
'x' | Exclusive Creation | Creates and opens for writing. | FileExistsError | Creates new file. |
What’s Next?
Choosing the correct file mode is the first step in safely and correctly interacting with files. It clearly signals your intent and can prevent disastrous data loss.
Now that we know how to open a file and in what mode, it’s time to actually get the data out of it. In Post #147, we will explore the different methods for reading content from a file, including .read()
, .readline()
, and .readlines()
.
Author

Experienced Cloud & DevOps Engineer with hands-on experience in AWS, GCP, Terraform, Ansible, ELK, Docker, Git, GitLab, Python, PowerShell, Shell, and theoretical knowledge on Azure, Kubernetes & Jenkins. In my free time, I write blogs on ckdbtech.com