29
loading...
This website collects cookies to deliver better user experience
dataset = fetch_data()
...
with open('customers.txt') as fh:
for each customer in dataset:
fh.write(...)
with open
) cleans up lingering resources for you, automatically closing out the file handle. Awesome. I see code like this all the time. But, can you spot the issue?from x import TempFile
# 1. create temporary file
with open(tempfile) as fh:
# 2. Write contents to file handle
fh.write(...)
# 3. Flush from any runtime or OS buffers
fh.flush()
# 4. Sync from memory to disk
os.fsync(fh.fileno())
# 5. Rename and replace destination file
os.rename(tempFile, "customers.txt")