Search code examples
pythonlinuxtemporary-files

Where does python tempfile writes its files?


In python you can create a tempfile as follows:

tempfile.TemporaryFile()

And then you can write to it. Where is the file written in a GNU/Linux system? I can't seem to find it in the /tmp directory or any other directory.

Thank you,


Solution

  • Looking at .name on a file handle is indeed one way to see where the file exists. In the case of TemporaryFile (on *NIX systems), you'll see <fdopen>, indicating an open file handle, but no corresponding directory entry. You'll need to use NamedTemporaryFile if you'd like to preserve the link to the underlying file.


    If you wish to control where temporary files go, look at the dir parameter:

    TemporaryFile uses mkstemp, which allows setting the directory with the dir parameter:

    If dir is specified, the file will be created in that directory; otherwise, a default directory is used. The default directory is chosen from a platform-dependent list, but the user of the application can control the directory location by setting the TMPDIR, TEMP or TMP environment variables.