How do I create a file in python without overwriting an existing file

前端 未结 3 1089
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-05 07:41

Currently I have a loop that tries to find an unused filename by adding suffixes to a filename string. Once it fails to find a file, it uses the name that failed to open a

3条回答
  •  北荒
    北荒 (楼主)
    2020-12-05 08:13

    If you have an id associated with each thread / process that tries to create the file, you could put that id in the suffix somewhere, thereby guaranteeing that no two processes can use the same file name.

    This eliminates the race condition between the processes.

提交回复
热议问题