Table Of Contents

Previous topic

QFile

Next topic

QTextStream

QTemporaryFile

Synopsis

Functions

Static functions

Detailed Description

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

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

The file name of the temporary file can be found by calling PySide.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 qt_temp 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, PySide.QtCore.QTemporaryFile appends the generated part to the filename given.

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

Constructs a PySide.QtCore.QTemporaryFile in QDir.tempPath() , using the file template “qt_temp.XXXXXX”. The file is stored in the system’s temporary directory.

Constructs a PySide.QtCore.QTemporaryFile (with the given parent ) in QDir.tempPath() , using the file template “qt_temp.XXXXXX”.

Constructs a PySide.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 PySide.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.

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

Returns true if the PySide.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 PySide.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 PySide.QtCore.QTemporaryFile.createLocalFile(fileName)
Parameters:fileName – unicode
Return type:PySide.QtCore.QTemporaryFile

This is an overloaded function.

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

static PySide.QtCore.QTemporaryFile.createLocalFile(file)
Parameters:filePySide.QtCore.QFile
Return type:PySide.QtCore.QTemporaryFile

If file is not on a local disk, a temporary file is created on a local disk, file is copied into the temporary local file, and a pointer to the temporary local file is returned. If file is already on a local disk, a copy is not created and 0 is returned.

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

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

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

A PySide.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 PySide.QtCore.QTemporaryFile.fileName() to the unique filename used.

See also

PySide.QtCore.QTemporaryFile.fileName()

PySide.QtCore.QTemporaryFile.setAutoRemove(b)
Parameters:bPySide.QtCore.bool

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

Auto-remove is on by default.

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

Sets the static portion of the file name to name . If the file template ends in 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.