Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 973
boatcoder Avatar asked Aug 28 '09 16:08

boatcoder


1 Answers

Use os.open() with os.O_CREAT and os.O_EXCL to create the file. That will fail if the file already exists:

>>> fd = os.open("x", os.O_WRONLY | os.O_CREAT | os.O_EXCL) Traceback (most recent call last):   File "<stdin>", line 1, in <module> OSError: [Errno 17] File exists: 'x' 

Once you've created a new file, use os.fdopen() to turn the handle into a standard Python file object:

>>> fd = os.open("y", os.O_WRONLY | os.O_CREAT | os.O_EXCL) >>> f = os.fdopen(fd, "w")  # f is now a standard Python file object 

Edit: From Python 3.3, the builtin open() has an x mode that means "open for exclusive creation, failing if the file already exists".

like image 162
RichieHindle Avatar answered Sep 21 '22 16:09

RichieHindle