Function parameters:
sptempdir.TemporaryDirectory(suffix="", prefix="", dir=None, delete=True)
By default temporary directory will be deleted when function it is closed.
The name
property returns the name of a temporary directory.
import os
from sptempdir import TemporaryDirectory
with TemporaryDirectory(prefix="prefbegin_", suffix="_suffend") as temp:
print('temp.name:', temp.name) # retrieve the name temporary directory
print('Inside:', os.path.exists(temp.name))
print('Outside:', os.path.exists(temp.name))
Terminal output:
$ temporary_directory.py
temp.name: /tmp/prefbegin_66XxiFkN6Nm4_suffend
Inside: True
Outside: False
import os
from sptempdir import TemporaryDirectory
temp = TemporaryDirectory()
print('temp.name:', temp.name) # retrieve the name temporary directory
print('Tempdir exists:', os.path.exists(temp.name))
temp.remove() # manually remove temporary directory
print('Tempdir exists:', os.path.exists(temp.name))
Terminal output:
$ temporary_directory.py
temp.name: /tmp/RCgAzfsATQnb
Tempdir exists: True
Tempdir exists: False
If the delete parameter is delete=False
, the temp directory is not deleted.
import os
from sptempdir import TemporaryDirectory
temp = TemporaryDirectory(delete=False)
print('temp.name:', temp.name) # retrieve the name temporary directory
print('Tempdir exists:', os.path.exists(temp.name))
temp.remove() # manually remove temporary directory
print('Tempdir exists:', os.path.exists(temp.name))
Terminal output:
$ temporary_directory.py
temp.name: /tmp/kWwCWn42NRsr
Tempdir exists: True
Tempdir exists: False
Specific dir
where you want to create temporary directory.
from sptempdir import TemporaryDirectory
temp = TemporaryDirectory(dir="/home/user/Desktop")
print(temp.name) # retrieve the name temporary directory
Terminal output:
$ temporary_directory.py
/home/user/Desktop/4ZdTvLNqVuyE
pip install sptempdir
BSD
(SP)TEMPDIR = ( Simple Python ) TEMPDIR