Cross-platform way of getting temp directory in Python
Asked Answered
L

5

380

Is there a cross-platform way of getting the path to the temp directory in Python 2.6?

For example, under Linux that would be /tmp, while under XP C:\Documents and settings\[user]\Application settings\Temp.

Lac answered 11/5, 2009 at 12:21 Comment(2)
not a pythonist, but you should use these methods for creating temporary files/directoriesUtah
See the tempfile module at docs.python.org/library/tempfile.html.Cheerless
S
535

That would be the tempfile module.

It has functions to get the temporary directory, and also has some shortcuts to create temporary files and directories in it, either named or unnamed.

Example:

import tempfile

print tempfile.gettempdir() # prints the current temporary directory

f = tempfile.TemporaryFile()
f.write('something on temporaryfile')
f.seek(0) # return to beginning of file
print f.read() # reads data back from the file
f.close() # temporary file is automatically deleted here

For completeness, here's how it searches for the temporary directory, according to the documentation:

  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.
Stringboard answered 11/5, 2009 at 12:25 Comment(3)
For me, OSX is putting it in /var/folders/<garbage/here> instead of /tmp because that is how $TMPDIR is set. See here.Cuesta
Currently, with python 3.6.5 on Windows 10, tempfile.gettempdir() resolves to C:\users\user\AppData\Local\Temp. An unfortunately long path.Erenow
@solvingJ: I don't think it has anything to do with Python, though. Which temp folder would be shorter?Desrochers
H
94

This should do what you want:

print(tempfile.gettempdir())

For me on my Windows box, I get:

c:\temp

and on my Linux box I get:

/tmp
Houstonhoustonia answered 11/5, 2009 at 12:27 Comment(3)
but this does not work for MacOS...Asclepius answer is for MacOS the better choiceInsobriety
@JohnMC It does work for MacOS, although MacOS reports a longer, less memorable path than other platformsOina
This gives ~\AppData\Local\Temp for me on Win 10 (~ = C:\Users\<you>)Retroaction
B
31

I use:

from pathlib import Path
import platform
import tempfile

tempdir = Path("/tmp" if platform.system() == "Darwin" else tempfile.gettempdir())

This is because on MacOS, i.e. Darwin, tempfile.gettempdir() and os.getenv('TMPDIR') return a value such as '/var/folders/nj/269977hs0_96bttwj2gs_jhhp48z54/T'; it is one that I do not always want.

Bullfrog answered 14/4, 2017 at 19:51 Comment(9)
At least in this case MacOS does the right thing of returning you an user-level isolated temp directory. I am 99.99% sure this is what you need.... unless you want to mess with the operating system.Nannana
@Nannana 99.99% is a stretch. I'd say 50% is more realistic. Often I'm working with multiprocessing and then I could want the same temp dir for all processes.Bullfrog
@Acumenus indeed as far as I know sorin is right. your assumption that the TMPDIR on osx will be different for each process is wrong. The TMPDIR is per user however per session. This location is cleaned up on reboot. If you want to convince yourself try a python code that display the TMPDIR and saved it to testTempdir.py Then do this in your terminal for ((i=0;i<20;i++)) do;./testTempDir.py&;echo $!;done. You’ll see 20 ≠ process id ans 20 times the same TMPDIRConcertize
@StephaneGasparini, true, but sometimes you need separate processes from separate parents (sessions), and this is more cross-platformJehanna
TMPDIR is per user so I'm not sure I'm catching what you meanConcertize
@StephaneGasparini Is it documented somewhere that TMPDIR on MacOS is per user?Bullfrog
@Bullfrog TMPDIR is an environment variable that express the result of NSTemporaryDirectory as far as I can tell. The API documentation says that it is per user see developer.apple.com/documentation/foundation/…. see also #10293848Concertize
If I'm looking for a platform-independent place to write logfiles, this answer seems like a reasonable choice?Variometer
@HughPerkins Lately I have been writing them to <project>/logs/. I created a <project>/logs/README.md file, so the directory doesn't disappear. I take care to ensure that old logs routinely get removed or rotated - this is important. This works for me.Bullfrog
S
26

The simplest way, based on @nosklo's comment and answer:

import tempfile
tmp = tempfile.mkdtemp()

But if you want to manually control the creation of the directories:

import os
from tempfile import gettempdir
tmp = os.path.join(gettempdir(), '.{}'.format(hash(os.times())))
os.makedirs(tmp)

That way you can easily clean up after yourself when you are done (for privacy, resources, security, whatever) with:

from shutil import rmtree
rmtree(tmp, ignore_errors=True)

This is similar to what applications like Google Chrome and Linux systemd do. They just use a shorter hex hash and an app-specific prefix to "advertise" their presence.

Succory answered 10/5, 2016 at 20:4 Comment(2)
you should use tempfile.mkdtemp() insteadStringboard
@nosklo, that's certainly an option, and would take advantage of all the robustness built into the tempfile package, but the hash approach lets you create a path of your choosing and nest multiple directories in a directory tree that meets your requirements. It's basically a more explicit, more flexible version of the mkdtemp() you suggested.Succory
S
-6

Why so many complex answers?

I just use this

   (os.getenv("TEMP") if os.name=="nt" else "/tmp") + os.path.sep + "tempfilename.tmp"
Shaky answered 8/3, 2021 at 10:20 Comment(2)
How is this less complex than other answers?Catalase
@CrispyHoliday It's a joke. They are joking.Leverage

© 2022 - 2024 — McMap. All rights reserved.