Skip to content

Instantly share code, notes, and snippets.

@hashbrowncipher
Created September 20, 2019 15:12
Show Gist options
  • Select an option

  • Save hashbrowncipher/35218438c4eb194f63a8fe73813e3095 to your computer and use it in GitHub Desktop.

Select an option

Save hashbrowncipher/35218438c4eb194f63a8fe73813e3095 to your computer and use it in GitHub Desktop.
Atomically creates a file in Linux
@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