top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Python: Does mkstemp open files only if they don't already exist?

0 votes
370 views

I assume the answer is "Yes", but is it safe to expect that tempfile.mkstemp() will only create a file that doesn't already exist? I presume that there's no chance of it over-writing an existing file (say, due to a race-condition).

posted Sep 1, 2015 by Vijay Shukla

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

It depends on OS support, but with that, yes, it is guaranteed to be safe; the file is opened with an exclusivity flag. Check your system's man pages for details, or here:

http://linux.die.net/man/3/open

O_EXCL|O_CREATE makes an "atomic file creation" operation which will fail if another process is doing the same thing. I'm not sure how mkstemp() handles that failure, but my guess/expectation is that it would pick a different file name and try again.

answer Sep 1, 2015 by Majula Joshi
Similar Questions
+4 votes

I am getting the following error -

UserProfile.picture: (fields.E210) Cannot use ImageField because Pillow is not installed.

HINT: Get Pillow at https://pypi.python.org/pypi/Pillow or run command "pip install pillow".
System check identified 1 issue (0 silenced).

This is installation Summary of pillow

PIL SETUP SUMMARY
version Pillow 2.6.1
platform linux 3.4.2 (default, Oct 8 2014, 13:08:17) [GCC 4.9.1]

--- TKINTER support available
--- JPEG support available
--- OPENJPEG (JPEG2000) support not available
--- ZLIB (PNG/ZIP) support available
--- LIBTIFF support available
--- FREETYPE2 support available
--- LITTLECMS2 support available
--- WEBP support available
--- WEBPMUX support available
+2 votes

I have an application which uses extensively python 2.7.6 (CPython).

The issue that I see is the following: "If there are only pyc files, the loading time of the application is much more than if I have pyc and py files. It is behind with 2 minutes more than if it had py files"

Do you have any idea why this is happening?

+3 votes

I am trying to count the number of files in a folder with python script but not getting the appropriate script. In which I can change the path and can find out the count of other folder files.

It doesn't matter for me how long the script is but need the clear script and it would be better if you can explain it also.

+1 vote

I was trying to program with txt files, and tried to move the txt file to a new directory. By mistake I have deleted some of my files. How do I recover my lost files using python or any other method?

...