In Python 3, TemporaryDirectory from the tempfile module can be used.
From the examples:
import tempfile
with tempfile.TemporaryDirectory() as tmpdirname:
print('created temporary directory', tmpdirname)
# directory and contents have been removed
To manually control when the directory is removed, don't use a context manager, as in the following example:
import tempfile
temp_dir = tempfile.TemporaryDirectory()
print(temp_dir.name)
# use temp_dir, and when done:
temp_dir.cleanup()
The documentation also says:
On completion of the context or destruction of the temporary directory object the newly created temporary directory and all its contents are removed from the filesystem.
At the end of the program, for example, Python will clean up the directory if it wasn't removed, e.g. by the context manager or the cleanup() method. Python's unittest may complain of ResourceWarning: Implicitly cleaning up <TemporaryDirectory... if you rely on this, though.
In Python 3, TemporaryDirectory from the tempfile module can be used.
From the examples:
import tempfile
with tempfile.TemporaryDirectory() as tmpdirname:
print('created temporary directory', tmpdirname)
# directory and contents have been removed
To manually control when the directory is removed, don't use a context manager, as in the following example:
import tempfile
temp_dir = tempfile.TemporaryDirectory()
print(temp_dir.name)
# use temp_dir, and when done:
temp_dir.cleanup()
The documentation also says:
On completion of the context or destruction of the temporary directory object the newly created temporary directory and all its contents are removed from the filesystem.
At the end of the program, for example, Python will clean up the directory if it wasn't removed, e.g. by the context manager or the cleanup() method. Python's unittest may complain of ResourceWarning: Implicitly cleaning up <TemporaryDirectory... if you rely on this, though.
Use the mkdtemp() function from the tempfile module:
import tempfile
import shutil
dirpath = tempfile.mkdtemp()
# ... do stuff with dirpath
shutil.rmtree(dirpath)
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:
- The directory named by the
TMPDIRenvironment variable.- The directory named by the
TEMPenvironment variable.- The directory named by the
TMPenvironment variable.- A platform-specific location:
- On RiscOS, the directory named by the
Wimp$ScrapDirenvironment 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.- As a last resort, the current working directory.
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
I found something interesting in the tempfile page. The function gettempdir gets the temporary folder from the following environment variable:
- The directory named by the TMPDIR environment variable.
- The directory named by the TEMP environment variable.
- The directory named by the TMP environment variable.
- A platform-specific location:
- 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.
- As a last resort, the current working directory.
Maybe you can overwrite one of this variable.
The options above didn't work for me. But after going through the code, I saw that it defines the tmpdir variable. It stores the path to the temporary directory. We change it to our own and that's it.
import tempfile
tempfile.tempdir = '/home/user/my_tmp'