Writing files requires careful handling of buffers, file creation flags, and atomic write operations to prevent file corruption during sudden system crashes.
'w': Overwrite file contents if file exists; create file if missing.'a': Append to end of file; create file if missing.'x': Exclusive creation; raises FileExistsError if file exists.from pathlib import Path
import tempfile
import os
def atomic_write(target_path: Path, content: str) -> None:
"""Safely write data using a temporary file to guarantee atomic writes."""
dir_path = target_path.parent
dir_path.mkdir(parents=True, exist_ok=True)
# Create temporary file in same directory
with tempfile.NamedTemporaryFile(mode='w', dir=dir_path, delete=False, encoding='utf-8') as tmp_file:
tmp_file.write(content)
tmp_path = Path(tmp_file.name)
# Atomic replace operation
tmp_path.replace(target_path)
if __name__ == "__main__":
out_file = Path("safe_output.json")
atomic_write(out_file, '{"status": "complete"}')
print(f"Atomically written file exists: {out_file.exists()}")
print("Content:", out_file.read_text(encoding="utf-8"))
out_file.unlink()
os.replace() to swap files atomically.file.flush() or os.fsync(file.fileno()) if critical data must be flushed from OS cache to physical disk immediately.Path(path).parent.mkdir(parents=True, exist_ok=True) before attempting to write to subdirectories.Write a function append_log(msg: str) that appends timestamped log lines to app.log.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With