Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I persist to disk a temporary file using Python?

I am attempting to use the 'tempfile' module for manipulating and creating text files. Once the file is ready I want to save it to disk. I thought it would be as simple as using 'shutil.copy'. However, I get a 'permission denied' IOError:

>>> import tempfile, shutil >>> f = tempfile.TemporaryFile(mode ='w+t') >>> f.write('foo') >>> shutil.copy(f.name, 'bar.txt')  Traceback (most recent call last):   File "<pyshell#5>", line 1, in <module>     shutil.copy(f.name, 'bar.txt')   File "C:\Python25\lib\shutil.py", line 80, in copy     copyfile(src, dst)   File "C:\Python25\lib\shutil.py", line 46, in copyfile     fsrc = open(src, 'rb') IOError: [Errno 13] Permission denied: 'c:\\docume~1\\me\\locals~1\\temp\\tmpvqq3go' >>>  

Is this not intended when using the 'tempfile' library? Is there a better way to do this? (Maybe I am overlooking something very trivial)

like image 932
Ray Avatar asked Sep 18 '08 16:09

Ray


People also ask

Where does Python store temporary files?

This function returns name of directory to store temporary files. This name is generally obtained from tempdir environment variable. On Windows platform, it is generally either user/AppData/Local/Temp or windowsdir/temp or systemdrive/temp. On linux it normally is /tmp.

Is Python Tempfile thread-safe?

tempfile can be used to create temporary files and directories in Python. These are thread-safe and automatically cleaned-up. tempfile provides file objects which can be used as context managers.


1 Answers

hop is right, and dF. is incorrect on why the error occurs.

Since you haven't called f.close() yet, the file is not removed.

The doc for NamedTemporaryFile says:

Whether the name can be used to open the file a second time, while the named temporary file is still open, varies across platforms (it can be so used on Unix; it cannot on Windows NT or later).

And for TemporaryFile:

Under Unix, the directory entry for the file is removed immediately after the file is created. Other platforms do not support this; your code should not rely on a temporary file created using this function having or not having a visible name in the file system.

Therefore, to persist a temporary file (on Windows), you can do the following:

import tempfile, shutil f = tempfile.NamedTemporaryFile(mode='w+t', delete=False) f.write('foo') file_name = f.name f.close() shutil.copy(file_name, 'bar.txt') os.remove(file_name) 

The solution Hans Sjunnesson provided is also off, because copyfileobj only copies from file-like object to file-like object, not file name:

shutil.copyfileobj(fsrc, fdst[, length])

Copy the contents of the file-like object fsrc to the file-like object fdst. The integer length, if given, is the buffer size. In particular, a negative length value means to copy the data without looping over the source data in chunks; by default the data is read in chunks to avoid uncontrolled memory consumption. Note that if the current file position of the fsrc object is not 0, only the contents from the current file position to the end of the file will be copied.

like image 122
K Z Avatar answered Oct 02 '22 15:10

K Z