Python Temporary Files and Directories: Difference between revisions
Line 36: | Line 36: | ||
==Auto-Deleting Temporary Directory== | ==Auto-Deleting Temporary Directory== | ||
A <code>TemporaryDirectory</code> creates a temporary directory on the filesystem. The underlying file system path can be obtained by wrapping the <code>TemporaryDirectory</code> into a <code>Path</code> instance. The underlying file system directory will be deleted as soon as the <code>TemporaryDirectory</code> object is closed, if <code>delete</code> is <code>true</code> (the default). The object is automatically closed when the control exits the context: | |||
<syntaxhighlight lang='py'> | |||
from pathlib import Path | |||
from tempfile import TemporaryDirectory | |||
with TemporaryDirectory() as td: | |||
temp_dir_path = Path(td) | |||
print(temp_dir_path.parent) | |||
print(temp_dir_path.name) | |||
assert temp_dir_path.is_dir() | |||
# | |||
# the directory will be deleted out of scope | |||
# | |||
assert not temp_dir_path.is_dir() | |||
</syntaxhighlight> | |||
=Temporary Files and Directories to be Deleted by the Caller= | =Temporary Files and Directories to be Deleted by the Caller= |
Revision as of 20:44, 13 June 2023
External
Internal
Auto-Deleting Temporary Files and Directories
TemporaryFile
, NamedTemporaryFile
and TemporaryDirectory
are classes that provide temporary files and directories that, in conjunction with the with
reserved word, are self-deleting when we the program gets out of scope.
Auto-Deleting Temporary File
A TemporaryFile
behaves "file-like", by exposing a file oriented-API to the underlying storage. NamedTemporaryFile
is similar to TemporaryFile
, except that the underlying file is guaranteed to have a visible name in the file system.
For NamedTemporaryFile
, the underlying file system path can be obtained with the name
property.
The underlying file system file will be deleted as soon as the NamedTemporaryFile
object is closed, if delete
is true
(the default). The object is automatically closed when the control exits the context:
from pathlib import Path
from tempfile import NamedTemporaryFile
content = 'something'
with NamedTemporaryFile(mode='w+t') as tf:
temp_file_path = tf.name
tf.write(content)
tf.flush()
# open the temporary file from the file system
with open(temp_file_path, 'r+t') as f:
content_from_file_system = f.read()
assert content == content_from_file_system
assert Path(temp_file_path).is_file()
#
# the file will be deleted out of scope
#
assert not Path(temp_file_path).is_file()
Auto-Deleting Temporary Directory
A TemporaryDirectory
creates a temporary directory on the filesystem. The underlying file system path can be obtained by wrapping the TemporaryDirectory
into a Path
instance. The underlying file system directory will be deleted as soon as the TemporaryDirectory
object is closed, if delete
is true
(the default). The object is automatically closed when the control exits the context:
from pathlib import Path
from tempfile import TemporaryDirectory
with TemporaryDirectory() as td:
temp_dir_path = Path(td)
print(temp_dir_path.parent)
print(temp_dir_path.name)
assert temp_dir_path.is_dir()
#
# the directory will be deleted out of scope
#
assert not temp_dir_path.is_dir()
Temporary Files and Directories to be Deleted by the Caller
Temporary File to be Deleted by the Caller
Temporary Directory to be Deleted by the Caller
Create a named temporary file, write in it and return the name. The file is not automatically deleted after close()
, the caller will need to delete it:
import tempfile
with tempfile.NamedTemporaryFile(mode='w+t', delete=False) as t:
t.write(something)
return t.name