This is an useful idiom:
- Create a (hidden) temporary file with random name.
- Write data to it.
- Finally, rename the temporary file to target filename.
This idiom is good for:
- Avoid creating incomplete file
- Support infile=outfile case for some tools. (e.g. Issue 33927: Allow json.tool to have identical infile and outfile - Python tracker)
As far as I know, Python stdlib don’t directly provide this idiom. Users need to implement it by themselves. But implement this idiom both for Unix and Windows is difficult.
By Googling, I found atomicwrite library. It support Unix and Windows.
I am not sure this library is good for stdlib. At least, we need to drop Python 2 support. I want to make fsync on directory optional too (see Option to disable fsync · Issue #17 · untitaker/python-atomicwrites · GitHub).
Anyway, I think this feature is good for stdlib. I want to add this feature regardless its implementation is
- Do you think Python stdlib provide this feature?
- Do you think the
atomicwriteAPI is good for stdlib?
- Which module should support this? (
os.path, or add
- Would you provide sample use cases that this feature is useful for many Python users?