August 2018
Intermediate to advanced
366 pages
10h 14m
English
You need to perform the following recipes:
import tempfile, os
class safe_open:
def __init__(self, path, mode='w+b'):
self._target = path
self._mode = mode
def __enter__(self):
self._file = tempfile.NamedTemporaryFile(self._mode, delete=False)
return self._file
def __exit__(self, exc_type, exc_value, traceback):
self._file.close()
if exc_type is None:
os.rename(self._file.name, self._target)
else:
os.unlink(self._file.name)
with safe_open('/tmp/myfile') as f: f.write(b'Hello ...