11.2 Replacing a file while preserving the previous version
We can leverage the power of the pathlib module to support a variety of filename manipulations. In the Using pathlib to work with filenames recipe in this chapter, we looked at a few of the most common techniques for managing directories, filenames, and file suffixes.
One common file processing requirement is to create output files in a fail-safe manner; that is, the application should preserve any previous output file, no matter how or where the application fails.
Consider the following scenario:
At time T0, there’s a valid output.csv file from a previous run of the long_complex.py application.
At time T1, we start running the long_complex.py application using new data. It begins overwriting the output.csv file. Until the program finishes, the bytes will be unusable.
At time T2, the application crashes. The...