Where does python tempfile writes its files?

前端 未结 3 1214
难免孤独
难免孤独 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 07:57

    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 , 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.

提交回复
热议问题