Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't Python `NamedTemporaryFile` default `delete` to `False`?

I was trying to use tempfile.NamedTemporaryFile in Python 3. I found that if I wanted to call any other process using subprocess.call or such by passing the new file name, I had to use delete = false, and then later on delete the file manually myself using os.unlink. I presume I have to close() the file before allowing another process to open it (in my case, calling a compiler to compile a source file created from Python), and apparently the file is deleted upon being closed.

I wonder, when Python already provides tempfile.TemporaryFile to produce temporary files which do not have names, of what use is a named temporary file if it is not to enable other processes to access them?

It seems to me that the default should be to not delete the created file upon the file being closed, and putting the responsibility of deleting the temporary file on the user, no? In fact, it seems that the delete argument is itself pointless because of this, since if the file will be deleted anyhow, then why do we need its name, so NamedTemporaryFile should never delete...

like image 551
jamadagni Avatar asked Oct 30 '15 07:10

jamadagni


People also ask

How do I delete temp files in Python?

gettempdir() to get the directory where all the temp files are stored. After running the program, if you go to temp_dir (which is /tmp in my case – Linux), you can see that the newly created file 3 is not there. This proves that Python automatically deletes these temporary files after they are closed.

Where is Tempfile stored python?

Practical Data Science using Python They are created in special temp directories that are defined by operating system file systems. For example, under Windows the temp folder resides in profile/AppData/Local/Temp while in linux the temporary files are held in /tmp directory.

What is NamedTemporaryFile?

This module creates temporary files and directories. It works on all supported platforms. TemporaryFile , NamedTemporaryFile , TemporaryDirectory , and SpooledTemporaryFile are high-level interfaces which provide automatic cleanup and can be used as context managers.

What does tmp mean in Python?

Tempfile is a Python module used in a situation, where we need to read multiple files, change or access the data in the file, and gives output files based on the result of processed data. Each of the output files produced during the program execution was no longer needed after the program was done.


1 Answers

It's an incorrect presumption that you have to close the file before other processes can access it. In fact, as you observed, when you call close on the NamedTemporaryFile it deletes the file on disk by default.

With tempfiles as with all of Python, scope and lifetime are all important. That's one of the great things about object orient languages, objects clean up after themselves when they're no longer needed (either deleted or go out of scope). Upon destruction, the tempfile objects free all resources in memory and named files on disk.

Thus, if you want to use the temporary file on disk by using the file system name, the NamedTemporaryFile should be in scope and unclosed. It's not guaranteed across all platforms, but on Unix/Linux the file should be accessible in the file system by other processes. However, the NamedTemporaryFile creates the file to readable and writeable only by the owner (unix permission 0600: -rw-------).

If you want/need to do more of this management and cleanup yourself, you might consider using the lower level function tempfile.mkstemp(). For example,

fd, tempfilename = tempfile.mkstemp()
f = os.fdopen(fd)

In either case be sure you flush the buffers before giving the filename to another process, file.flush().

like image 84
RobertL Avatar answered Oct 11 '22 15:10

RobertL