Atomically creating a file if it doesn't exist in Python
问题 I am looking for an atomic version of the following: import os def tryMakeFile(filename): try: with open(filename) as _: return False except FileNotFoundError: with open(filename, mode='a') as _: return True (Please don't comment on stylistic issues here - I know this code is bad in many ways, but it suffices to illustrate my question.) In other words, I'm looking for a way to check if a file exists, and create it if it doesn't, in Python, in such a way that I know which happened. But done in