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
Argument | Type | Required | |
---|---|---|---|
path | str | os.PathLike | None | The 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 a file 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
- Codes
- Results
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)
Suppose that the script returns
/tmp/tmp6xikuxe7/test.txt
Since the temporary folder will be removed. After running this program, calling
ls /tmp/tmp6xikuxe7/test.txt
will report a file missing message.
Create a temporary directory with a specific path
- Codes
- Results
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)
Suppose that the script returns
./temp/test.txt
Since the temporary folder will be removed. After running this program, calling
ls ./temp/test.txt
will report a file missing message.