June 2019
Beginner to intermediate
770 pages
19h 24m
English
In this section, we'll discuss a more complex context manager that attempts some cleanup when there are problems.
This addresses the common issue where we want to save a backup copy of a file that our application is rewriting. We want to be able to do something similar to the following:
with Updating(some_path):
with some_path.open('w') as target_file:
process(target_file)
The intent is to have the original file renamed to some_file copy. If the context works normally, that is, no exceptions are raised, then the backup copy can be deleted or renamed to some_file old.
If the context doesn't work normally, that is, an exception is raised, we want to rename the new file to some_file error and rename the old ...
Read now
Unlock full access