Why with open(…) as … in python
In python, when trying to open a file, we can always see something like this:
with open(path) as f:
What is the meaning? Why we use it in this way?
Python’s “with open(…) as …” Pattern
Reading and writing data to files using Python is pretty straightforward. To do this, you must first open files in the appropriate mode. Here’s an example of how to use Python’s “with open(…) as …” pattern to open a text file and read its contents:
with open('data.txt', 'r') as f:
data = f.read()
open() takes a filename and a mode as its arguments.
r opens the file in read only mode. To write data to a file, pass in
w as an argument instead:
with open('data.txt', 'w') as f:
data = 'some data to be written to the file'
In the examples above,
open() opens files for reading or writing and returns a file handle (
fin this case) that provides methods that can be used to read or write data to the file. Read Working With File I/O in Python for more information on how to read and write to files.
Something interesting is about text file is shown here from 
A text file on the other hand, has no specific encoding and can be opened by a standard text editor without any special handling. Still, every text file must adhere to a set of rules:
Text files have to be readable as is. They can (and often do) contain a lot of special encoding, especially in HTML or other markup languages, but you’ll still be able to tell what it says
Data in a text file is organized by lines. In most cases, each line is a distinct element, whether it’s a line of instruction or a command.
Additionally, text files all have an unseen character at the end of each line which lets the text editor know that there should be a new line. When interacting with these files through programming, you can take advantage of that character. In Python, it is denoted by the “
The mode in the open function tells Python what you want to do with the file. There are multiple modes that you can specify when dealing with text files.
'w'– Write Mode: This mode is used when the file needs to be altered and information changed or added. Keep in mind that this erases the existing file to create a new one. File pointer is placed at the beginning of the file.
'r'– Read Mode: This mode is used when the information in the file is only meant to be read and not changed inany way. File pointer is placed at the beginning of the file.
'a'– Append Mode: This mode adds information to the end of the file automatically. File pointer is placed at the end of the file.
'r+'– Read/Write Mode: This is used when you will be making changes to the file and reading information from it. The file pointer is placed at the beginning of the file.
'a+'– Append and Read Mode: A file is opened to allow data to be added to the end of the file and lets your program read information as well. File pointer is placed at the end of the file.
with open("workData.txt", "r+") as workData:
# File object is now open.
# Do stuff with the file:
# File object is now closed.
# Do other things...
If you don’t use the
withkeyword or use the
fileobject.close()function then Python will automatically close and destroy the file object through the built in garbage collector. However, depending on your code, this garbage collection can happen at any time.
So it’s recommended to use the
withkeyword in order to control when the file will be closed—namely after the inner code block finishes executing.
The bolded one in the above should be the answer of why we are using “with open(…) as …” to handle the file IOs.