PySide6.QtCore.QTemporaryFile¶
- class QTemporaryFile¶
- The - QTemporaryFileclass is an I/O device that operates on temporary files. More…- Synopsis¶- Methods¶- def - __init__()
- def - autoRemove()
- def - fileTemplate()
- def - open()
- def - setAutoRemove()
 - Static functions¶- 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. - QTemporaryFileis used to create unique temporary files safely. The file itself is created by calling- 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- QTemporaryFileobject. 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- 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 - QTemporaryFileafter calling- close()is safe. For as long as the- QTemporaryFileobject itself is not destroyed, the unique temporary file will exist and be kept open internally by- QTemporaryFile.- 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()(otherwise- qt_temp) and will be placed into the temporary path as returned by- 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.- It is important to specify the correct directory if the - rename()function will be called, as- QTemporaryFilecan only rename files within the same volume / filesystem as the temporary file itself was created on.- The file name (the part after the last directory path separator in the specified file template) can contain the special sequence - "XXXXXX"(at least six upper case- "X"characters), which will be replaced with the auto-generated portion of the file name. If the file name doesn’t contain- "XXXXXX",- QTemporaryFilewill append the generated part to the file name. Only the last occurrence of- "XXXXXX"will be considered.- Note - On Linux, - QTemporaryFilewill attempt to create unnamed temporary files. If that succeeds,- open()will return true but- exists()will be false. If you call- fileName()or any function that calls it,- QTemporaryFilewill give the file a name, so most applications will not see a difference.- See also - __init__()¶
 - Constructs a - QTemporaryFile.- The default file name template is determined from the application name as returned by - applicationName()(or- "qt_temp"if the application name is empty), followed by- ".XXXXXX". The file is stored in the system’s temporary directory, as returned by- tempPath().- See also - setFileTemplate()- fileTemplate()- fileName()- tempPath()- __init__(parent)
- Parameters:
- parent – - QObject
 
 - Constructs a - QTemporaryFilewith the given- parent.- The default file name template is determined from the application name as returned by - applicationName()(or- "qt_temp"if the application name is empty), followed by- ".XXXXXX". The file is stored in the system’s temporary directory, as returned by- tempPath().- See also - __init__(templateName)
- Parameters:
- templateName – str 
 
 - Constructs a - QTemporaryFilewith- templateNameas the file name template.- Upon opening the temporary file, - templateNamewill be used to create a unique filename.- If the file name (the part after the last directory path separator in - templateName) doesn’t contain- "XXXXXX", it will be added automatically.- "XXXXXX"will be replaced with the dynamic part of the file name, which is calculated to be unique.- If - templateNameis a relative path, the path will be relative to the current working directory. You can use- tempPath()to construct- templateNameif you want use the system’s temporary directory.- It is important to specify the correct directory if the - rename()function will be called, as- QTemporaryFilecan only rename files within the same volume / filesystem as the temporary file itself was created on.- See also - __init__(templateName, parent)
- Parameters:
- templateName – str 
- parent – - QObject
 
 
 - Constructs a - QTemporaryFilewith the specified- parent, and- templateNameas the file name template.- Upon opening the temporary file, - templateNamewill be used to create a unique filename.- If the file name (the part after the last directory path separator in - templateName) doesn’t contain- "XXXXXX", it will be added automatically.- "XXXXXX"will be replaced with the dynamic part of the file name, which is calculated to be unique.- If - templateNameis a relative path, the path will be relative to the current working directory. You can use- tempPath()to construct- templateNameif you want use the system’s temporary directory. It is important to specify the correct directory if the- rename()function will be called, as- QTemporaryFilecan only rename files within the same volume / filesystem as the temporary file itself was created on.- See also - autoRemove()¶
- Return type:
- bool 
 
 - Returns - trueif the- QTemporaryFileis in auto remove mode. Auto-remove mode will automatically delete the filename from disk upon destruction. This makes it very easy to create your- QTemporaryFileobject 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 - Warning - This section contains snippets that were automatically translated from C++ to Python and may contain errors. - If - fileis not already a native file, then a- QTemporaryFileis created in- tempPath(), the contents of- fileis copied into it, and a pointer to the temporary file is returned. Does nothing and returns- 0if- fileis already a native file.- For example: - f = QFile(":/resources/file.txt") QTemporaryFile.createNativeFile(f) # Returns a pointer to a temporary file f = QFile("/users/qt/file.txt") QTemporaryFile.createNativeFile(f) # Returns 0 - See also - static createNativeFile(fileName)
- Parameters:
- fileName – str 
- Return type:
 
 - This is an overloaded function. - Works on the given - fileNamerather than an existing- QFileobject.- fileTemplate()¶
- Return type:
- str 
 
 - Returns the file name template. - The file name template returned by this method, will be relative or absolute depending on the file name template used to construct this object (or passed to - setFileTemplate()) being relative or absolute, respectively.- See also - setFileTemplate()- fileName()- Default File Name Template- open()¶
- Return type:
- bool 
 
 - Opens a unique temporary file in the file system in - ReadWritemode. Returns- trueif the file was successfully opened, or was already open. Otherwise returns- false.- If called for the first time, open() will create a unique file name based on - fileTemplate(). The file is guaranteed to have been created by this function (that is, it has never existed before).- If a file is reopened after calling - close(), the same file will be opened again.- See also - setFileTemplate()- QT_USE_NODISCARD_FILE_OPEN- setAutoRemove(b)¶
- Parameters:
- b – bool 
 
 - Sets the - QTemporaryFileinto auto-remove mode if- bis- 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- fileName()function to obtain the name and never try to guess the name that- QTemporaryFilehas 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 call- fileName()or leave the auto removal functionality enabled.- See also - setFileTemplate(name)¶
- Parameters:
- name – str 
 
 - Sets the file name template to - templateName.- If the file name (the part after the last directory path separator in - templateName) doesn’t contain- "XXXXXX", it will be added automatically.- "XXXXXX"will be replaced with the dynamic part of the file name, which is calculated to be unique.- If - templateNameis a relative path, the path will be relative to the current working directory. You can use- tempPath()to construct- templateNameif you want use the system’s temporary directory. It is important to specify the correct directory if the- rename()function will be called, as- QTemporaryFilecan only rename files within the same volume / filesystem as the temporary file itself was created on.- See also - fileTemplate()- fileName()