Skip to main content

TempDir

ClassSource

tempdir = TempDir(path: str | os.PathLike | None = None)

Temporary directory.

Will remove the entire directory when this instance is destructed.

Acquired from

https://docs.python.org/3/library/weakref.html#comparing-finalizers-with-del-methods

Arguments

ArgumentTypeRequired
Description
pathstr | os.PathLike | NoneThe path to the temporary directory. If this path is absent, will use tempfile.mkdtemp() to create a file in the temporary folder.

Methods

remove

tempdir.remove()

Remove this folder explicitly. If it has been removed, this method will not take effects.

Note that this method only works if it is called by the main process.

Properties

path

path: str = tempdir.path

The path of this temporary folder.


is_removed

is_removed: bool = tempdir.is_removed

Check whether this temporary folder has been removed.

Examples

Create a temporary directory in the system's temporary folder

import os
from dash_file_cache.utilities import TempDir

tmp_dir = TempDir()
file_path = os.path.join(tmp_dir.path, "test.txt")
with open(file_path, "w") as fobj:
fobj.write("test")

print(file_path)

Create a temporary directory with a specific path

import os
from dash_file_cache.utilities import TempDir

tmp_dir = TempDir("./temp")
file_path = os.path.join(tmp_dir.path, "test.txt")
with open(file_path, "w") as fobj:
fobj.write("test")

print(file_path)