QTemporaryFile¶
The
QTemporaryFile
class is an I/O device that operates on temporary files. More…
Synopsis¶
Functions¶
def
autoRemove
()def
fileTemplate
()def
open
()def
setAutoRemove
(b)def
setFileTemplate
(name)
Static functions¶
def
createLocalFile
(file)def
createLocalFile
(fileName)def
createNativeFile
(file)def
createNativeFile
(fileName)
Detailed Description¶
QTemporaryFile
is used to create unique temporary files safely. The file itself is created by callingopen()
. The name of the temporary file is guaranteed to be unique (i.e., you are guaranteed to not overwrite an existing file), and the file will subsequently be removed upon destruction of theQTemporaryFile
object. This is an important technique that avoids data corruption for applications that store data in temporary files. The file name is either auto-generated, or created based on a template, which is passed toQTemporaryFile
‘s constructor.Example:
# Within a function/method... file_ = QTemporaryFile() if file_.open(): # file_.fileName() returns the unique file name # The QTemporaryFile destructor removes the temporary file # as it goes out of scope.Reopening a
QTemporaryFile
after callingclose()
is safe. For as long as theQTemporaryFile
object itself is not destroyed, the unique temporary file will exist and be kept open internally byQTemporaryFile
.The file name of the temporary file can be found by calling
fileName()
. Note that this is only defined after the file is first opened; the function returns an empty string before this.A temporary file will have some static part of the name and some part that is calculated to be unique. The default filename will be determined from
applicationName()
(otherwiseqt_temp
) and will be placed into the temporary path as returned bytempPath()
. If you specify your own filename, a relative file path will not be placed in the temporary directory by default, but be relative to the current working directory. It is important to specify the correct directory if therename()
function will be called, asQTemporaryFile
can only rename files within the same volume / filesystem as the temporary file itself was created on.Specified filenames can contain the following template
XXXXXX
(six upper case “X” characters), which will be replaced by the auto-generated portion of the filename. Note that the template is case sensitive. If the template is not present in the filename,QTemporaryFile
appends the generated part to the filename given.Note
On Linux,
QTemporaryFile
will attempt to create unnamed temporary files. If that succeeds,open()
will return true butexists()
will be false. If you callfileName()
or any function that calls it,QTemporaryFile
will give the file a name, so most applications will not see a difference.See also
- class PySide2.QtCore.QTemporaryFile¶
PySide2.QtCore.QTemporaryFile(parent)
PySide2.QtCore.QTemporaryFile(templateName)
PySide2.QtCore.QTemporaryFile(templateName, parent)
- param templateName:
str
- param parent:
Constructs a
QTemporaryFile
using as file template the application name returned byapplicationName()
(otherwiseqt_temp
) followed by “.XXXXXX”. The file is stored in the system’s temporary directory,tempPath()
.See also
Constructs a
QTemporaryFile
(with the givenparent
) using as file template the application name returned byapplicationName()
(otherwiseqt_temp
) followed by “.XXXXXX”. The file is stored in the system’s temporary directory,tempPath()
.See also
Constructs a
QTemporaryFile
with a template filename oftemplateName
and the specifiedparent
. Upon opening the temporary file this will be used to create a unique filename.If the
templateName
does not contain XXXXXX it will automatically be appended and used as the dynamic portion of the filename.If
templateName
is a relative path, the path will be relative to the current working directory. You can usetempPath()
to constructtemplateName
if you want use the system’s temporary directory. It is important to specify the correct directory if therename()
function will be called, asQTemporaryFile
can only rename files within the same volume / filesystem as the temporary file itself was created on.See also
- PySide2.QtCore.QTemporaryFile.autoRemove()¶
- Return type:
bool
Returns
true
if theQTemporaryFile
is in auto remove mode. Auto-remove mode will automatically delete the filename from disk upon destruction. This makes it very easy to create yourQTemporaryFile
object on the stack, fill it with data, read from it, and finally on function return it will automatically clean up after itself.Auto-remove is on by default.
See also
- static PySide2.QtCore.QTemporaryFile.createLocalFile(file)¶
- Parameters:
file –
PySide2.QtCore.QFile
- Return type:
Note
This function is deprecated.
- static PySide2.QtCore.QTemporaryFile.createLocalFile(fileName)
- Parameters:
fileName – str
- Return type:
Note
This function is deprecated.
- static PySide2.QtCore.QTemporaryFile.createNativeFile(file)¶
- Parameters:
file –
PySide2.QtCore.QFile
- Return type:
- static PySide2.QtCore.QTemporaryFile.createNativeFile(fileName)
- Parameters:
fileName – str
- Return type:
- PySide2.QtCore.QTemporaryFile.fileTemplate()¶
- Return type:
str
Returns the set file template. The default file template will be called qcoreappname.XXXXXX and be placed in
tempPath()
.See also
- PySide2.QtCore.QTemporaryFile.open()¶
- Return type:
bool
A
QTemporaryFile
will always be opened inReadWrite
mode, this allows easy access to the data in the file. This function will return true upon success and will set thefileName()
to the unique filename used.See also
fileName()
- PySide2.QtCore.QTemporaryFile.setAutoRemove(b)¶
- Parameters:
b – bool
Sets the
QTemporaryFile
into auto-remove mode ifb
istrue
.Auto-remove is on by default.
If you set this property to
false
, ensure the application provides a way to remove the file once it is no longer needed, including passing the responsibility on to another process. Always use thefileName()
function to obtain the name and never try to guess the name thatQTemporaryFile
has generated.On some systems, if
fileName()
is not called before closing the file, the temporary file may be removed regardless of the state of this property. This behavior should not be relied upon, so application code should either callfileName()
or leave the auto removal functionality enabled.See also
- PySide2.QtCore.QTemporaryFile.setFileTemplate(name)¶
- Parameters:
name – str
Sets the static portion of the file name to
name
. If the file template contains XXXXXX that will automatically be replaced with the unique part of the filename, otherwise a filename will be determined automatically based on the static portion specified.If
name
contains a relative file path, the path will be relative to the current working directory. You can usetempPath()
to constructname
if you want use the system’s temporary directory. It is important to specify the correct directory if therename()
function will be called, asQTemporaryFile
can only rename files within the same volume / filesystem as the temporary file itself was created on.See also
© 2022 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.