15

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,

Loic Duros
  • 5,472
  • 10
  • 43
  • 56
  • 4
    ["Under Unix, the directory entry for the file is removed immediately after the file is created."](http://docs.python.org/2/library/tempfile.html#tempfile.TemporaryFile). Careful with that. – user2357112 Aug 16 '13 at 18:57
  • 1
    when I try: mytempfile.name I get: "" – Loic Duros Aug 16 '13 at 19:20

3 Answers3

13

Call the tempfile.gettempdir() function:

Return the directory currently selected to create temporary files in.

You can change where temporary files are created by setting the tempfile.tempdir value to different directory if you want to influence where temporary files are created. Quoting from the documentation, if that value is None the rules are as follows:

If tempdir is unset or None at any call to any of the above functions, Python searches a standard list of directories and sets tempdir to the first one which the calling user can create files in. The list is:

  1. The directory named by the TMPDIR environment variable.
  2. The directory named by the TEMP environment variable.
  3. The directory named by the TMP environment variable.
  4. A platform-specific location:
    • On RiscOS, the directory named by the Wimp$ScrapDir environment variable.
    • On Windows, the directories C:\TEMP, C:\TMP, \TEMP, and \TMP, in that order.
    • On all other platforms, the directories /tmp, /var/tmp, and /usr/tmp, in that order.
  5. As a last resort, the current working directory.
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
7

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.

David Cain
  • 16,484
  • 14
  • 65
  • 75
  • The file.name returns "". Does it make sense? – Loic Duros Aug 16 '13 at 19:16
  • 2
    Yes, it does. `TemporaryFile` just deals with the OS [file descriptor](https://en.wikipedia.org/wiki/File_descriptor), so there's no accessible file name. If you want a file with an accessible path, use a [`NamedTemporaryFile`](http://docs.python.org/2/library/tempfile.html#tempfile.NamedTemporaryFile). Reading the documentation for [`mkstemp`](http://docs.python.org/2/library/tempfile.html#tempfile.mkstemp) will hopefully shed some light on this and ease any confusion. – David Cain Aug 16 '13 at 19:24
  • 2
    @Loic: It means it is an open file handle and the underlying file has already been unlinked. Use `tempfile.NamedTemporaryFile()` if you need a filename. – Martijn Pieters Aug 16 '13 at 19:24
  • 3
    @Loic: On POSIX systems, you can unlink a file while you still have a file handle. Your process can read and write to the file, but no other process can access it because the directory entry has already been removed. – Martijn Pieters Aug 16 '13 at 19:25
  • 2
    @Loic: This is highly a desirable feature, making your temporary file a secure place to store information that other processes are not supposed to be able to read, let alone write to. – Martijn Pieters Aug 16 '13 at 19:27
2

on the shell try this

    $python
>>> import tempfile
    >>>tempfile.gettempdir()

'/var/folders/5r/lxh8g5ln7zg43_l_3t0j7b1c0000gn/T'

This will show you the actual temp path python is using.

You can optionally try this. (this will produce the same result as above)

    $python
>>> import tempfile
    >>>tempfile.tempdir
'/var/folders/5r/lxh8g5ln7zg43_l_3t0j7b1c0000gn/T'
Prashant
  • 21
  • 1