PySide6.QtCore.QTemporaryDir¶
- class QTemporaryDir¶
The
QTemporaryDir
class creates a unique directory for temporary use. More…Synopsis¶
Methods¶
def
__init__()
def
autoRemove()
def
errorString()
def
filePath()
def
isValid()
def
path()
def
remove()
def
setAutoRemove()
def
swap()
Note
This documentation may contain snippets that were automatically translated from C++ to Python. We always welcome contributions to the snippet translation. If you see an issue with the translation, you can also let us know by creating a ticket on https:/bugreports.qt.io/projects/PYSIDE
Detailed Description¶
Warning
This section contains snippets that were automatically translated from C++ to Python and may contain errors.
QTemporaryDir
is used to create unique temporary directories safely. The directory itself is created by the constructor. The name of the temporary directory is guaranteed to be unique (i.e., you are guaranteed to not overwrite an existing directory), and the directory will subsequently be removed upon destruction of theQTemporaryDir
object. The directory name is either auto-generated, or created based on a template, which is passed toQTemporaryDir
‘s constructor.Example:
# Within a function/method... dir = QTemporaryDir() if dir.isValid(): # dir.path() returns the unique directory path # The QTemporaryDir destructor removes the temporary directory # as it goes out of scope.
It is very important to test that the temporary directory could be created, using
isValid()
. Do not useexists()
, since a default-constructedQDir
represents the current directory, which exists.The path to the temporary directory can be found by calling
path()
.A temporary directory will have some static part of the name and some part that is calculated to be unique. The default path will be determined from
applicationName()
(otherwiseqt_temp
) and will be placed into the temporary path as returned bytempPath()
. If you specify your own path, a relative path will not be placed in the temporary directory by default, but be relative to the current working directory. In all cases, a random string will be appended to the path in order to make it unique.See also
- __init__()¶
Constructs a
QTemporaryDir
using as template the application name returned byapplicationName()
(otherwiseqt_temp
). The directory is stored in the system’s temporary directory,tempPath()
.See also
- __init__(templateName)
- Parameters:
templateName – str
Constructs a
QTemporaryDir
with a template oftemplatePath
.If
templatePath
is a relative path, the path will be relative to the current working directory. You can usetempPath()
to constructtemplatePath
if you want use the system’s temporary directory.If the
templatePath
ends with XXXXXX it will be used as the dynamic portion of the directory name, otherwise it will be appended. UnlikeQTemporaryFile
, XXXXXX in the middle of the template string is not supported.See also
- autoRemove()¶
- Return type:
bool
Returns
true
if theQTemporaryDir
is in auto remove mode. Auto-remove mode will automatically delete the directory from disk upon destruction. This makes it very easy to create yourQTemporaryDir
object on the stack, fill it with files, do something with the files, and finally on function return it will automatically clean up after itself.Auto-remove is on by default.
See also
- errorString()¶
- Return type:
str
If
isValid()
returnsfalse
, this function returns the error string that explains why the creation of the temporary directory failed. Otherwise, this function return an empty string.- filePath(fileName)¶
- Parameters:
fileName – str
- Return type:
str
Returns the path name of a file in the temporary directory. Does not check if the file actually exists in the directory. Redundant multiple separators or “.” and “..” directories in
fileName
are not removed (seecleanPath()
). Absolute paths are not allowed.The returned path will be relative or absolulte depending on whether
QTemporaryDir
was constructed with a relative or absolute path, respectively.- isValid()¶
- Return type:
bool
Returns
true
if theQTemporaryDir
was created successfully.- path()¶
- Return type:
str
Returns the path to the temporary directory. Empty if the
QTemporaryDir
could not be created.The returned path will be relative or absolulte depending on whether
QTemporaryDir
was constructed with a relative or absolute path, respectively.- remove()¶
- Return type:
bool
Removes the temporary directory, including all its contents.
Returns
true
if removing was successful.- setAutoRemove(b)¶
- Parameters:
b – bool
Sets the
QTemporaryDir
into auto-remove mode ifb
is true.Auto-remove is on by default.
See also
- swap(other)¶
- Parameters:
other –
QTemporaryDir
Swaps temporary-dir
other
with this temporary-dir. This operation is very fast and never fails.