Hard
All or Nothing File Write
Write
atomic_write(name) with @contextmanager: it yields a file opened on name + '.tmp'; if the block finishes, it renames the temp file over name with os.replace; if an error happens it deletes the temp file and lets the error continue. The file data.txt starts as old. Read lines until END; write them inside atomic_write, but raise ValueError if a line is BAD. Catch it outside, then print the file's contents and whether the .tmp file exists.Input
Lines, then END.
Output
Lines.
Sample 1
Input
new lines END
Output
new lines False
Sample 2
Input
a BAD b END
Output
error: bad line old False
Output appears here