With Statement in Python (35/100 Days of Python)
The with
statement in Python is used to work with resources that need to be properly managed, such as files, sockets, and databases. The with
statement provides a convenient way to ensure that resources are properly acquired and released, even in the case of exceptions.
The Basic Syntax of the with Statement
The basic syntax of the with
statement includes the with
keyword followed by an expression and then as
and a variable:
with expression [as variable]:
...
The expression
is an object that is being managed, such as a file or a database connection. The variable
is an optional identifier that can be used to refer to the object within the with
block.
Reading From a File Using the With Statement
One common use case for the with
statement is reading a file. With the with
statement, you can ensure that the file is properly closed, even if an exception occurs while reading the file:
with open('example.txt', 'r') as f:
contents = f.read()
# Do something with the contents of the file
In this example, the file example.txt
is opened using the open
function, and a reference to the file object is stored in the variable f
. Within the with
block, the contents of the file are read using the read
method and stored in the variable contents
. When the with
block is exited, the file is automatically closed, even if an exception occurs.
Writing to a File Using the With Statement
The with
statement can also be used when writing to a file:
with open('example.txt', 'w') as f:
f.write('Hello, World!')
In this example, the file example.txt
is opened using the open
function in write mode, and a reference to the file object is stored in the variable f
. Within the with
block, the string "Hello, World!"
is written to the file using the write
method. When the with
block is exited, the file is automatically closed, even if an exception occurs.
The with
statement in Python is a convenient way to manage resources that need to be properly acquired and released, such as files, sockets, and databases (which we will discuss later in the series). With the with
statement, you can ensure that resources are properly closed, even if an exception occurs, and avoid potential bugs related to resource management. Whether you are reading from or writing to a file, the with
statement can help you write cleaner and more robust code.
Working With Multiple Files
When working with multiple files, the with
statement can be used to manage each file separately, making it easier to handle errors and exceptions that may occur.
Let’s consider a real-world scenario where we need to read data from two different files and write the combined data to a third file. In this scenario, the with
statement can be used to manage each of the files, as shown below:
with open('names.txt', 'r') as name_file, open('surnames.txt', 'r') as surname_file:
names = name_file.read()
surnames = surname_file.read()
with open('full-names.txt', 'w') as f:
for name, surname in zip(names, surnames):
f.write(f'{name} {surname}\n')
In this example, the first with
statement opens two files, names.txt
and surnames.txt
, in read mode ('r'
) and stores references to the file objects in the variables name_file
and surname_file
, respectively. Within the with
block, the contents of both files are read and stored in the variables names
and surnames
.
When the with
block is exited, both the names.txt
and surnames.txt
files are automatically closed, even if an exception occurs while reading the data.
The second with
statement opens a file full-names.txt
in write mode ('w'
) and stores a reference to the file object in the variable f
. Within the with
block, the contents of names
and surnames
are written to the full-names.txt
file using the write
method line-by-line.
Similarly, when the second with
block is exited, the full-names.txt
file is automatically closed.
With Statements Enclosed in Parentheses
Starting from the 3.10 version, Python allows using enclose the with
statements in parentheses: reference.
This means that we can have multiple context managers on different lines as easily as:
with (open('first.txt', 'r') as f1):
...
with (
open('second', 'r'),
open('third', 'w'),
):
...
with (open('one.txt') as one,
open('two.txt')):
...
with (
open('three.txt', 'w') as three,
open('four.txt', 'r') as four
):
...
This is especially useful when working with many files at the same time.
with
statements are actually more powerful and provide flexibility to work with much more than only files. We’ll touch on those topics and how they can be used with databases or sockets in the coming series.
What’s next?
- If you found this story valuable, please consider clapping multiple times (this really helps a lot!)
- Hands-on Practice: Free Python Course —
with
statement in Python - Full series: 100 Days of Python
- Previous topic: Working With Files
- Next topic: Automating Data Cleaning With Python