The with statement as a context manager

In real-life applications, it is rather easy to mismanage opened files in your programs by forgetting to close them; it can sometimes also be the case that it is impossible to tell whether the program has finished processing a file, and we programmers will therefore be unable to make a decision as to when to put the statement to close the files appropriately. This situation is even more common in concurrent and parallel programming, where the order of execution between different elements changes frequently.

One possible solution to this problem that is also common in other programming languages is to use a try...except...finally block every time we want to interact with an external file. This solution still requires the same level of management and significant overhead and does not provide a good improvement in the ease and readability of our programs either. This is when the with statement of Python comes into play.

The with statement gives us a simple way of ensuring that all opened files are properly managed and cleaned up when the program finishes using them. The most notable advantage of using the with statement comes from the fact that, even if the code is successfully executed or it returns an error, the with statement always handles and manages the opened files appropriately via context. For example, let's look at our Chapter04/example1.py file in more detail:

# Chapter04/example1.py

n_files = 254
files = []

# method 3
for i in range(n_files):
with open('output1/sample%i.txt' % i, 'w') as f:
files.append(f)

While this method accomplishes the same job as the second method we saw earlier, it additionally provides a cleaner and more readable way to manage the opened files that our program interacts with. More specifically, the with statement helps us indicate the scope of certain variables—in this case, the variables that point to the opened files—and hence, their context.

For example, in the third method in the preceding code, the f variable indicates the current opened file within the with block at each iteration of the for loop, and as soon as our program exits that with block (which is outside the scope of that f variable), there is no longer any other way to access it. This architecture guarantees that all cleanup associated with a file descriptor happens appropriately. The with statement is hence called a context manager.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.139.97.202