In Post #144, we learned how to tell Python where to find a file using absolute and relative paths. Now we’re ready for the next step: actually opening that file to read or write data.
While there is a basic open()
and close()
pattern, the modern, safe, and universally recommended way to work with files in Python is with the with
statement. In this post, you’ll learn why this is the only way you should be handling files.
The Old Way: Manual open()
and close()
To understand why the with
statement is so important, let’s first look at the manual way of handling files. The process involves two explicit steps: calling the built-in open()
function to get a file object, and then remembering to call the .close()
method on that object when you are finished.
# The manual, less safe way
file_handle = open("hello.txt", "w") # 'w' opens the file for writing
file_handle.write("Hello from the old way!")
file_handle.close()
The Danger of the Manual Method
This seems simple, but it has a major flaw. What if an error occurs in your code after the file is opened but before the .close()
line is reached?
file_handle = open("data.txt", "w")
# Imagine an unexpected error happens here, like 1 / 0
file_handle.close() # This line would never be executed!
In that case, your program would crash, and the .close()
method would never be called. The file would be left “dangling” open by your program. This can lead to data corruption or, in larger applications, exhaust system resources by leaving too many files open.
The Pythonic Solution: The with open()
Statement
To solve this problem and guarantee that your files are always closed properly, Python provides the with
statement. The with
statement creates something called a context manager.
A context manager’s job is to manage the setup and teardown of a resource. For files, it guarantees that the cleanup action (closing the file) is performed no matter what, even if an error occurs inside the with
block.
Think of it like a self-cleaning kitchen: you do your cooking inside the with
block, and the moment you step out, the kitchen automatically cleans itself up (closes the file), regardless of whether you successfully cooked your meal or accidentally set off the fire alarm.
The with
Statement in Action
The syntax is clean, readable, and should be your default for all file operations.
with open(“filename.txt”, “mode”) as file_variable:
Let’s break it down:
with
: The keyword that starts the context manager.open(...)
: The function that opens the file. We’ll pass it the path and a “mode” (like"w"
for write).as file_variable
: This takes the file object returned byopen()
and assigns it to a variable (a common convention isf
orfile
) that you can use inside thewith
block.
Here is the safe and modern way to write to a file:
file_path = "my_novel.txt"
# The 'w' means we are opening the file in 'write' mode.
with open(file_path, "w") as file:
print("File is open inside the 'with' block.")
file.write("Chapter 1: It was a dark and stormy night.")
# No need to call file.close()!
# As soon as the indented block is exited, Python automatically closes the file.
print("The 'with' block is finished, and the file is now closed.")
This code is robust. Even if an error happened inside the with
block, Python would still ensure the file was closed properly before the program crashed.
What’s Next?
The with open()
statement is the standard for file handling in modern Python. It provides a safe, readable, and error-proof way to ensure your files are always closed properly, preventing resource leaks and potential data corruption. From now on, you should use it every time you work with files.
In our examples, we used "w"
when opening the file. This is the “mode,” and it told Python we intended to write to the file. But what if we want to read a file, or add to the end of an existing one? In Post #146, we will explore the different file modes, like 'r'
, 'w'
, and 'a'
.
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