In our last couple of posts (Post #147 Post #148), we’ve focused entirely on reading data from files. Now, we’ll learn how to do the opposite: write data to files. This is the key to making our programs save their state, create reports, or generate log files.
In this post, we’ll cover the two main modes for writing to text files: write mode ('w'
) for creating a new file or overwriting an existing one, and append mode ('a'
) for adding to the end of a file.
Writing to a File with ‘w’ Mode
To write content to a file, we use the .write()
method on a file object that has been opened in 'w'
mode.
As a crucial reminder from Post #146, opening an existing file in 'w'
mode will immediately delete all of its previous content. If the file doesn’t exist, it will be created for you.
The .write()
Method
The .write()
method takes a single string as its argument and writes it to the file. A very important difference from the print()
function is that .write()
does not automatically add a newline character at the end. You must add the newline character \n
yourself if you want to start a new line.
Let’s create a new file and write two lines to it.
file_path = "my_story.txt"
with open(file_path, "w") as file:
file.write("Chapter 1: The Beginning\n")
file.write("It was a bright, sunny day.\n")
print(f"'{file_path}' has been written.")
If you now open my_story.txt
in a text editor, you will see:
Chapter 1: The Beginning
It was a bright, sunny day.
If you run this Python script a second time with different text, it will completely overwrite the original content.
Appending to a File with ‘a’ Mode
Overwriting a file every time isn’t always what we want. If you need to add new information to the end of a file while keeping its existing content, you need append mode ('a'
).
When you open a file in 'a'
mode, the cursor is placed at the end of the file. Any calls to .write()
will add new content from that point forward. If the file doesn’t exist, it will be created.
This is perfect for adding entries to a log file.
log_file = "app.log"
# Let's start a new log file using 'w' mode
with open(log_file, "w") as f:
f.write("Log file started.\n")
# Now, let's append some events using 'a' mode
with open(log_file, "a") as f:
f.write("Event 1: User logged in.\n")
f.write("Event 2: Data processed successfully.\n")
If you inspect app.log
, you will see that all the content is there:
Log file started.
Event 1: User logged in.
Event 2: Data processed successfully.
If you run the second with
block again, it will add those two lines to the end of the file again, without deleting the previous entries.
Writing Multiple Lines with .writelines()
If you have a list of strings that you want to write to a file, you could use a for
loop with .write()
. Alternatively, Python provides the .writelines()
method for this purpose.
The .writelines()
method takes an iterable (like a list) of strings and writes each string to the file in sequence.
Crucial Point: Just like .write()
, this method does not add newline characters for you. Your strings in the list must already contain them if you want each on a separate line.
lines_to_write = ["First line\n", "Second line\n", "Third line\n"]
with open("writelines_example.txt", "w") as f:
f.writelines(lines_to_write)
What’s Next?
You now have the fundamental tools for writing data to text files. You can create or overwrite files with 'w'
mode and safely add to them with 'a'
mode, using the .write()
and .writelines()
methods to save your program’s data.
So far, we have been representing file paths using simple strings. While this works, it can be a bit clumsy, especially when dealing with different operating systems (like the \
vs. /
issue). Modern Python offers a more powerful, object-oriented way to handle file system paths. In Post #150, we will be introduced to the pathlib
module.
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