Atomicwrites
Description:
- Race-free assertion that the target file doesn’t yet exist.
Resources:
- [GitHub] https://github.com/untitaker/python-atomicwrites
- [Docs] https://python-atomicwrites.readthedocs.io/en/latest/#
Note:
- Whenever you are writing files to local filesystem, you cannot write to the ‘/’ directory or you will get a permission denied error. Make a temporary directory first.
Example 1
Section titled “Example 1”Write a file to the local filesystem
from atomicwrites import atomic_writefrom pathlib import Path
# Create the dirtmp_path = "/tmp/ganymede_demo"path = Path(tmp_path)path.mkdir(parents=True, exist_ok=True)
# Define file pathfile_ = "foo.txt"fp = f"{tmp_path}/{file_}"
# Write the file to localwith atomic_write(fp, overwrite=True) as f: f.write('Hello world.') # "foo.txt" doesn't exist yet
# Now it does. Check it's contentswith open(fp, "r") as f: print(f.read())ganymede is made by Eugo