Created
September 20, 2019 15:12
-
-
Save hashbrowncipher/35218438c4eb194f63a8fe73813e3095 to your computer and use it in GitHub Desktop.
Atomically creates a file in Linux
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @contextmanager | |
| def atomic_temporary_file(destination, *, mode="w+b", flags=0, **kwargs): | |
| fd = os.open(destination.parent, os.O_TMPFILE | os.O_RDWR | flags) | |
| with io.open(fd, mode, **kwargs) as fh: | |
| yield fh | |
| os.link("/proc/self/fd/{}".format(fd), str(destination), src_dir_fd=0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment