This module creates temporary files and directories. It works on all supported platforms. TemporaryFile, NamedTemporaryFile, TemporaryDirectory, and SpooledTemporaryFile are high-level interfaces which provide automatic cleanup and can be used as context managers. mkstemp() and mkdtemp() are lower-level functions which require manual cleanup.
import os
import tempfile
from pathlib import Path
tempfile.TemporaryFile¶
fp = tempfile.TemporaryFile()
fp.write(b"how are you")
fp.seek(0)
fp.read()
fp.close()
type(fp)
fp.name
with tempfile.TemporaryFile() as fp:
fp.write(b"how are you")
fp.seek(0)
print(fp.read())
tempfile.TemporaryDirectory¶
Notice that after finish of the with
block,
the temporary directory is removed automatically.
with tempfile.TemporaryDirectory() as tempdir:
print(tempdir)
type(tempdir)
Path(tempdir).exists()
tempfile.mkstemp¶
The returned file descriptor must be closed (using
os.close
manually or using a contextual manager) to avoid leak of resource!The
suffix
option can be used to create a temporary file with a specific file extension.
Below are examples of using tempfile.mkstemp
in correct ways.
The first example close the file descriptor manually using os.close
.
The second example close the file automatically using the with block.
fd, file = tempfile.mkstemp()
print(file)
os.write(fd, b"how are you")
os.close(fd)
!cat {file}
fd, file = tempfile.mkstemp()
print(file)
with os.fdopen(fd, "w") as fout:
fout.write("I am fine.")
!cat {file}
Create a temporary SQL file.
tempfile.mkstemp(suffix=".sql")
tempfile.mkdtemp¶
import tempfile
tempfile.mkdtemp()
Set a Different Temporary Directory¶
In certain situations,
you might want to set a different temporary directory than the default one.
For example,
if an application leverages temporary files/dirs extensively
while there's very limited space in the temporary directory
(commonly seen when the application is running in a container environment on cloud),
you can run into "OSError: No space left on the device".
An easy fix of this issue is to set environment variables to point the temporary directory
to an existing directory with enough space which is writable by the user.
The code below illustrate how to change the default temporary directory.
Notice that tempfile.tempdir = None
is added to clear possible caching.
For more details,
please refer to
tempfile.gettempdir
.
import tempfile
import os
from pathlib import Path
tempfile.tempdir = None
os.environ["TMPDIR"] = str(Path.home())
tempfile.mkstemp()