How do I create a file in python without overwriting an existing file
file, multithreading, python
Solution
If you are concerned about a race condition, you can create a temporary file and then rename it.
>>> import os
>>> import tempfile
>>> f = tempfile.NamedTemporaryFile(delete=False)
>>> f.name
'c:\\users\\hughdb~1\\appdata\\local\\temp\\tmpsmdl53'
>>> f.write("Hello world")
>>> f.close()
>>> os.rename(f.name, r'C:\foo.txt')
>>> if os.path.exists(r'C:\foo.txt') :
... print 'File exists'
...
File exists
Alternatively, you can create the files using a uuid in the name. Stackoverflow item on this.
>>> import uuid
>>> str(uuid.uuid1())
'64362370-93ef-11de-bf06-0023ae0b04b8'
Problem
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 new file wit that name. Problem is this code is used in a website and there could be multiple attempts to do the same thing at the same time, so a race condition exists. How can I keep python from overwriting an existing file, if one is created between the time of the check and the time of the open in the other thread. I can minimize the chance by randomizing the suffixes, but the chance is already minimized based on parts of the pathname. I want to eliminate that chance with a function that can be told, create this file ONLY if it doesn't exist. I can use win32 functions to do this, but I want this to work cross platform because it will be hosted on linux in the end.