QTemporaryFile

Inheritance diagram of PySide2.QtCore.QTemporaryFile

Synopsis

Functions

Static functions

Detailed Description

The PySide2.QtCore.QTemporaryFile class is an I/O device that operates on temporary files.

PySide2.QtCore.QTemporaryFile is used to create unique temporary files safely. The file itself is created by calling PySide2.QtCore.QTemporaryFile.open() . 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 the PySide2.QtCore.QTemporaryFile 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 to PySide2.QtCore.QTemporaryFile ‘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 PySide2.QtCore.QTemporaryFile after calling PySide2.QtCore.QFileDevice.close() is safe. For as long as the PySide2.QtCore.QTemporaryFile object itself is not destroyed, the unique temporary file will exist and be kept open internally by PySide2.QtCore.QTemporaryFile .

The file name of the temporary file can be found by calling PySide2.QtCore.QTemporaryFile.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 QCoreApplication.applicationName() (otherwise qt_temp ) and will be placed into the temporary path as returned by QDir.tempPath() . 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.

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, PySide2.QtCore.QTemporaryFile appends the generated part to the filename given.

class PySide2.QtCore.QTemporaryFile
class PySide2.QtCore.QTemporaryFile(parent)
class PySide2.QtCore.QTemporaryFile(templateName)
class PySide2.QtCore.QTemporaryFile(templateName, parent)
Parameters:

Constructs a PySide2.QtCore.QTemporaryFile using as file template the application name returned by QCoreApplication.applicationName() (otherwise qt_temp ) followed by “.XXXXXX”. The file is stored in the system’s temporary directory, QDir.tempPath() .

Constructs a PySide2.QtCore.QTemporaryFile (with the given parent ) using as file template the application name returned by QCoreApplication.applicationName() (otherwise qt_temp ) followed by “.XXXXXX”. The file is stored in the system’s temporary directory, QDir.tempPath() .

Constructs a PySide2.QtCore.QTemporaryFile with a template filename of templateName . 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 use QDir.tempPath() to construct templateName if you want use the system’s temporary directory.

Constructs a PySide2.QtCore.QTemporaryFile with a template filename of templateName and the specified parent . 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 use QDir.tempPath() to construct templateName if you want use the system’s temporary directory.

PySide2.QtCore.QTemporaryFile.autoRemove()
Return type:PySide2.QtCore.bool

Returns true if the PySide2.QtCore.QTemporaryFile is in auto remove mode. Auto-remove mode will automatically delete the filename from disk upon destruction. This makes it very easy to create your PySide2.QtCore.QTemporaryFile 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.

static PySide2.QtCore.QTemporaryFile.createLocalFile(fileName)
Parameters:fileName – unicode
Return type:PySide2.QtCore.QTemporaryFile

This is an overloaded function.

Use QTemporaryFile.createNativeFile (const PySide2.QtCore.QString & PySide2.QtCore.QTemporaryFile.fileName() ) instead.

static PySide2.QtCore.QTemporaryFile.createLocalFile(file)
Parameters:filePySide2.QtCore.QFile
Return type:PySide2.QtCore.QTemporaryFile

Use QTemporaryFile.createNativeFile ( PySide2.QtCore.QFile &file) instead.

static PySide2.QtCore.QTemporaryFile.createNativeFile(file)
Parameters:filePySide2.QtCore.QFile
Return type:PySide2.QtCore.QTemporaryFile

If file is not already a native file, then a PySide2.QtCore.QTemporaryFile is created in QDir.tempPath() , the contents of file is copied into it, and a pointer to the temporary file is returned. Does nothing and returns 0 if file is already a native file.

For example:

QFile f(":/resources/file.txt");
QTemporaryFile::createNativeFile(f); // Returns a pointer to a temporary file

QFile f("/users/qt/file.txt");
QTemporaryFile::createNativeFile(f); // Returns 0
static PySide2.QtCore.QTemporaryFile.createNativeFile(fileName)
Parameters:fileName – unicode
Return type:PySide2.QtCore.QTemporaryFile

This is an overloaded function.

Works on the given fileName rather than an existing PySide2.QtCore.QFile object.

PySide2.QtCore.QTemporaryFile.fileTemplate()
Return type:unicode

Returns the set file template. The default file template will be called qcoreappname.XXXXXX and be placed in QDir.tempPath() .

PySide2.QtCore.QTemporaryFile.open()
Return type:PySide2.QtCore.bool

A PySide2.QtCore.QTemporaryFile will always be opened in QIODevice.ReadWrite mode, this allows easy access to the data in the file. This function will return true upon success and will set the PySide2.QtCore.QTemporaryFile.fileName() to the unique filename used.

See also

PySide2.QtCore.QTemporaryFile.fileName()

PySide2.QtCore.QTemporaryFile.setAutoRemove(b)
Parameters:bPySide2.QtCore.bool

Sets the PySide2.QtCore.QTemporaryFile into auto-remove mode if b is true .

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 the PySide2.QtCore.QTemporaryFile.fileName() function to obtain the name and never try to guess the name that PySide2.QtCore.QTemporaryFile has generated.

On some systems, if PySide2.QtCore.QTemporaryFile.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 call PySide2.QtCore.QTemporaryFile.fileName() or leave the auto removal functionality enabled.

PySide2.QtCore.QTemporaryFile.setFileTemplate(name)
Parameters:name – unicode

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 use QDir.tempPath() to construct name if you want use the system’s temporary directory.