Where does python tempfile writes its files?

前端 未结 3 1187
难免孤独
难免孤独 2021-01-12 07:42

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/L

3条回答
  •  孤街浪徒
    2021-01-12 08:15

    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.

提交回复
热议问题