tempfile.TemporaryFile
doesn't have a name that you can get. In particular:
Under Unix, the directory entry for the file is removed immediately after the file is created.
On other platforms, there may or may not be a directory entry for the file, but you still have no way of getting it from the API.
This is exactly what tempfile.NamedTemporaryFile
is for:
This function operates exactly as
TemporaryFile()
does, except that the file is guaranteed to have a visible name in the file system (on Unix, the directory entry is not unlinked). That name can be retrieved from thename
attribute of the file object.
Note that even NamedTemporaryFile
will unlink the name as soon as you close it, unless you pass delete=False
. So, you have two choices:
- Use
delete=False
and rely on the OS to clean up the file at some point in the future. (On OS X, with the default per-user temp system, I believe the folder will get scanned and cleaned either at next logout or at next login, which man not be for a long time.) - Wait until the child process has opened the file before you close it. The obvious way to do that is to use a blocking
subprocess.check_call
or the like instead of just firing-and-forgetting aPopen
, but if you want to get sneakier, you can. For example, you can do the waiting-and-deleting in a background thread. Or, if this script is part of a larger suite of scripts, you can have it pass the temp path upward (via stdout, a file, whatever) and have the last script clean up all the files. Or you could just find a way to wait for the file to be opened instead of waiting for the whole process to be finished. Or patchmongoimport
to add a--unlink-input-file
flag. Or…