继承者: QTemporaryFile
PySide.QtCore.QFile class provides an interface for reading from and writing to files.
PySide.QtCore.QFile is an I/O device for reading and writing text and binary files and resources . A PySide.QtCore.QFile may be used by itself or, more conveniently, with a PySide.QtCore.QTextStream or PySide.QtCore.QDataStream .
The file name is usually passed in the constructor, but it can be set at any time using PySide.QtCore.QFile.setFileName() . PySide.QtCore.QFile expects the file separator to be ‘/' regardless of operating system. The use of other separators (e.g., ‘') is not supported.
You can check for a file's existence using PySide.QtCore.QFile.exists() , and remove a file using PySide.QtCore.QFile.remove() . (More advanced file system related operations are provided by PySide.QtCore.QFileInfo and PySide.QtCore.QDir .)
The file is opened with PySide.QtCore.QFile.open() , closed with PySide.QtCore.QFile.close() , and flushed with PySide.QtCore.QFile.flush() . Data is usually read and written using PySide.QtCore.QDataStream or PySide.QtCore.QTextStream , but you can also call the PySide.QtCore.QIODevice -inherited functions PySide.QtCore.QIODevice.read() , PySide.QtCore.QIODevice.readLine() , PySide.QtCore.QIODevice.readAll() , PySide.QtCore.QIODevice.write() . PySide.QtCore.QFile also inherits PySide.QtCore.QIODevice.getChar() , PySide.QtCore.QIODevice.putChar() ,和 PySide.QtCore.QIODevice.ungetChar() , which work one character at a time.
The size of the file is returned by PySide.QtCore.QFile.size() . You can get the current file position using PySide.QtCore.QFile.pos() , or move to a new file position using PySide.QtCore.QFile.seek() . If you've reached the end of the file, PySide.QtCore.QFile.atEnd() returns true.
The following example reads a text file line by line:
file = QFile("in.txt")
if not file.open(QIODevice.ReadOnly | QIODevice.Text):
return
while not file.atEnd():
line = file.readLine() # A QByteArray
process_line(line)
QIODevice.Text flag passed to PySide.QtCore.QFile.open() tells Qt to convert Windows-style line terminators (“rn”) into C++-style terminators (“n”). By default, PySide.QtCore.QFile assumes binary, i.e. it doesn't perform any conversion on the bytes stored in the file.
The next example uses PySide.QtCore.QTextStream to read a text file line by line:
file = QFile("in.txt")
if not file.open(QIODevice.ReadOnly | QIODevice.Text):
return
in = QTextStream(file)
while not in.atEnd():
line = in.readLine() # A QByteArray
process_line(line)
PySide.QtCore.QTextStream takes care of converting the 8-bit data stored on disk into a 16-bit Unicode PySide.QtCore.QString . By default, it assumes that the user system's local 8-bit encoding is used (e.g., ISO 8859-1 for most of Europe; see QTextCodec.codecForLocale() for details). This can be changed using setCodec().
To write text, we can use operator<<(), which is overloaded to take a PySide.QtCore.QTextStream on the left and various data types (including PySide.QtCore.QString ) on the right:
file = QFile("out.txt")
if not file.open(QIODevice.WriteOnly | QIODevice.Text):
return
out = QTextStream(file)
out << "The magic number is: " << 49 << "\n"
PySide.QtCore.QDataStream is similar, in that you can use operator<<() to write data and operator>>() to read it back. See the class documentation for details.
When you use PySide.QtCore.QFile , PySide.QtCore.QFileInfo ,和 PySide.QtCore.QDir to access the file system with Qt, you can use Unicode file names. On Unix, these file names are converted to an 8-bit encoding. If you want to use standard C++ APIs ( <cstdio> or <iostream> ) or platform-specific APIs to access files instead of PySide.QtCore.QFile , you can use the PySide.QtCore.QFile.encodeName() and PySide.QtCore.QFile.decodeName() functions to convert between Unicode file names and 8-bit file names.
On Unix, there are some special system files (e.g. in /proc ) for which PySide.QtCore.QFile.size() will always return 0, yet you may still be able to read more data from such a file; the data is generated in direct response to you calling PySide.QtCore.QIODevice.read() . In this case, however, you cannot use PySide.QtCore.QFile.atEnd() to determine if there is more data to read (since PySide.QtCore.QFile.atEnd() will return true for a file that claims to have size 0). Instead, you should either call PySide.QtCore.QIODevice.readAll() , or call PySide.QtCore.QIODevice.read() or PySide.QtCore.QIODevice.readLine() repeatedly until no more data can be read. The next example uses PySide.QtCore.QTextStream to read /proc/modules line by line:
file = QFile("/proc/modules")
if not file.open(QIODevice.ReadOnly | QIODevice.Text):
return
in = QTextStream(file)
line = in.readLine()
while not line.isNull():
process_line(line)
line = in.readLine()
File permissions are handled differently on Linux/Mac OS X and Windows. In a non writable directory on Linux, files cannot be created. This is not always the case on Windows, where, for instance, the ‘My Documents' directory usually is not writable, but it is still possible to create files in it.
| 参数: |
|
|---|
构造新文件对象采用给定 parent .
构造新文件对象以表示文件采用给定 name .
构造新文件对象采用给定 parent to represent the file with the specified name .
此枚举描述可能的错误,错误返回通过 PySide.QtCore.QFile.error() 函数。
| 常量 | 描述 |
|---|---|
| QFile.NoError | 没有发生错误。 |
| QFile.ReadError | An error occurred when reading from the file. |
| QFile.WriteError | An error occurred when writing to the file. |
| QFile.FatalError | 发生致命错误。 |
| QFile.ResourceError | |
| QFile.OpenError | The file could not be opened. |
| QFile.AbortError | The operation was aborted. |
| QFile.TimeOutError | 发生超时。 |
| QFile.UnspecifiedError | An unspecified error occurred. |
| QFile.RemoveError | The file could not be removed. |
| QFile.RenameError | The file could not be renamed. |
| QFile.PositionError | The position in the file could not be changed. |
| QFile.ResizeError | The file could not be resized. |
| QFile.PermissionsError | The file could not be accessed. |
| QFile.CopyError | The file could not be copied. |
This enum is used when opening a file to specify additional options which only apply to files and not to a generic PySide.QtCore.QIODevice .
| 常量 | 描述 |
|---|---|
| QFile.AutoCloseHandle | The file handle passed into PySide.QtCore.QFile.open() should be closed by PySide.QtCore.QFile.close() , the default behaviour is that close just flushes the file and the application is responsible for closing the file handle. When opening a file by name, this flag is ignored as Qt always “owns” the file handle and must close it. |
| QFile.DontCloseHandle | The file handle passed into PySide.QtCore.QFile.open() will not be closed by Qt. The application must ensure that PySide.QtCore.QFile.close() 被调用。 |
注意
This enum was introduced or modified in Qt 4.8
This enum describes special options that may be used by the PySide.QtCore.QFile.map() 函数。
| 常量 | 描述 |
|---|---|
| QFile.NoOptions | 没有选项。 |
This enum is used by the permission() function to report the permissions and ownership of a file. The values may be OR-ed together to test multiple permissions and ownership values.
| 常量 | 描述 |
|---|---|
| QFile.ReadOwner | The file is readable by the owner of the file. |
| QFile.WriteOwner | The file is writable by the owner of the file. |
| QFile.ExeOwner | The file is executable by the owner of the file. |
| QFile.ReadUser | 文件对于用户是可读的。 |
| QFile.WriteUser | 文件对于用户是可写的。 |
| QFile.ExeUser | The file is executable by the user. |
| QFile.ReadGroup | The file is readable by the group. |
| QFile.WriteGroup | The file is writable by the group. |
| QFile.ExeGroup | The file is executable by the group. |
| QFile.ReadOther | The file is readable by anyone. |
| QFile.WriteOther | The file is writable by anyone. |
| QFile.ExeOther | The file is executable by anyone. |
警告
Because of differences in the platforms supported by Qt, the semantics of ReadUser , WriteUser and ExeUser are platform-dependent: On Unix, the rights of the owner of the file are returned and on Windows the rights of the current user are returned. This behavior might change in a future Qt version.
Note that Qt does not by default check for permissions on NTFS file systems, as this may decrease the performance of file handling considerably. It is possible to force permission checking on NTFS by including the following code in your source:
extern Q_CORE_EXPORT int qt_ntfs_permission_lookup;
Permission checking is then turned on and off by incrementing and decrementing qt_ntfs_permission_lookup by 1.
qt_ntfs_permission_lookup += 1 // turn checking on
qt_ntfs_permission_lookup += 1 // turn it off again
| 参数: | newName – unicode |
|---|---|
| 返回类型: | PySide.QtCore.bool |
拷贝目前指定的文件通过 PySide.QtCore.QFile.fileName() to a file called newName . Returns true if successful; otherwise returns false.
注意:若文件采用名称 newName already exists, PySide.QtCore.QFile.copy() returns false (i.e. PySide.QtCore.QFile 不会覆写它)。
关闭源文件在拷贝它之前。
| 参数: |
|
|---|---|
| 返回类型: |
PySide.QtCore.bool |
这是重载函数。
拷贝文件 fileName to newName . Returns true if successful; otherwise returns false.
若文件采用名称 newName already exists, PySide.QtCore.QFile.copy() returns false (i.e., PySide.QtCore.QFile 不会覆写它)。
| 参数: | localFileName – str |
|---|---|
| 返回类型: | unicode |
这是重载函数。
返回 Unicode 版本为给定 localFileName 。见 PySide.QtCore.QFile.encodeName() 了解细节。
| 参数: | localFileName – PySide.QtCore.QByteArray |
|---|---|
| 返回类型: | unicode |
这做反向 QFile.encodeName() 使用 localFileName .
另请参阅
setDecodingFunction() PySide.QtCore.QFile.encodeName()
| 参数: | fileName – unicode |
|---|---|
| 返回类型: | PySide.QtCore.QByteArray |
By default, this function converts fileName to the local 8-bit encoding determined by the user's locale. This is sufficient for file names that the user chooses. File names hard-coded into the application should only use 7-bit ASCII filename characters.
另请参阅
PySide.QtCore.QFile.decodeName() setEncodingFunction()
| 返回类型: | PySide.QtCore.QFile.FileError |
|---|
Returns the file error status.
I/O 设备状态返回错误代码。例如,若 PySide.QtCore.QFile.open() returns false, or a read/write operation returns -1, this function can be called to find out the reason why the operation failed.
| 参数: | fileName – unicode |
|---|---|
| 返回类型: | PySide.QtCore.bool |
Returns true if the file specified by fileName exists; otherwise returns false.
| 返回类型: | PySide.QtCore.bool |
|---|
这是重载函数。
Returns true if the file specified by PySide.QtCore.QFile.fileName() exists; otherwise returns false.
| 返回类型: | PySide.QtCore.QAbstractFileEngine |
|---|
Returns the QIOEngine for this PySide.QtCore.QFile 对象。
| 返回类型: | unicode |
|---|
返回名称,设置通过 PySide.QtCore.QFile.setFileName() or to the PySide.QtCore.QFile 构造函数。
| 返回类型: | PySide.QtCore.bool |
|---|
Flushes any buffered data to the file. Returns true if successful; otherwise returns false.
| 返回类型: | PySide.QtCore.int |
|---|
Returns the file handle of the file.
This is a small positive integer, suitable for use with C library functions such as fdopen() and fcntl(). On systems that use file descriptors for sockets (i.e. Unix systems, but not Windows) the handle can be used with PySide.QtCore.QSocketNotifier as well.
If the file is not open, or there is an error, PySide.QtCore.QFile.handle() returns -1.
This function is not supported on Windows CE.
On Symbian, this function returns -1 if the file was opened normally, as Symbian OS native file handles do not fit in an int, and are incompatible with C library functions that the handle would be used for. If the file was opened using the overloads that take an open C library file handle / file descriptor, then this function returns that same handle.
| 参数: |
|
|---|---|
| 返回类型: |
PySide.QtCore.bool |
这是重载函数。
Creates a link named linkName that points to the file fileName . What a link is depends on the underlying filesystem (be it a shortcut on Windows or a symbolic link on Unix). Returns true if successful; otherwise returns false.
| 参数: | newName – unicode |
|---|---|
| 返回类型: | PySide.QtCore.bool |
Creates a link named linkName that points to the file currently specified by PySide.QtCore.QFile.fileName() . What a link is depends on the underlying filesystem (be it a shortcut on Windows or a symbolic link on Unix). Returns true if successful; otherwise returns false.
This function will not overwrite an already existing entity in the file system; in this case, link() will return false and set PySide.QtCore.QFile.error() to return RenameError .
注意
To create a valid link on Windows, linkName must have a .lnk file extension.
注意
Symbian filesystem does not support links.
| 参数: |
|
|---|---|
| 返回类型: |
PyObject |
Maps size bytes of the file into memory starting at offset . A file should be open for a map to succeed but the file does not need to stay open after the memory has been mapped. When the PySide.QtCore.QFile is destroyed or a new file is opened with this object, any maps that have not been unmapped will automatically be unmapped.
Any mapping options can be passed through flags .
Returns a pointer to the memory or 0 if there is an error.
注意
On Windows CE 5.0 the file will be closed before mapping occurs.
| 参数: |
|
|---|---|
| 返回类型: |
PySide.QtCore.bool |
| 参数: |
|
|---|---|
| 返回类型: |
PySide.QtCore.bool |
| 返回类型: | PySide.QtCore.QFile.Permissions |
|---|
Returns the complete OR-ed together combination of QFile.Permission for the file.
| 参数: | filename – unicode |
|---|---|
| 返回类型: | PySide.QtCore.QFile.Permissions |
这是重载函数。
Returns the complete OR-ed together combination of QFile.Permission for fileName .
| 返回类型: | unicode |
|---|
| 参数: | fileName – unicode |
|---|---|
| 返回类型: | unicode |
| 返回类型: | PySide.QtCore.bool |
|---|
Removes the file specified by PySide.QtCore.QFile.fileName() . Returns true if successful; otherwise returns false.
The file is closed before it is removed.
| 参数: | fileName – unicode |
|---|---|
| 返回类型: | PySide.QtCore.bool |
这是重载函数。
Removes the file specified by the fileName 给定。
Returns true if successful; otherwise returns false.
| 参数: | newName – unicode |
|---|---|
| 返回类型: | PySide.QtCore.bool |
Renames the file currently specified by PySide.QtCore.QFile.fileName() to newName . Returns true if successful; otherwise returns false.
若文件采用名称 newName already exists, PySide.QtCore.QFile.rename() returns false (i.e., PySide.QtCore.QFile 不会覆写它)。
The file is closed before it is renamed.
| 参数: |
|
|---|---|
| 返回类型: |
PySide.QtCore.bool |
这是重载函数。
Renames the file oldName to newName . Returns true if successful; otherwise returns false.
若文件采用名称 newName already exists, PySide.QtCore.QFile.rename() returns false (i.e., PySide.QtCore.QFile 不会覆写它)。
| 参数: |
|
|---|---|
| 返回类型: |
PySide.QtCore.bool |
这是重载函数。
集 fileName to size (in bytes) sz . Returns true if the file if the resize succeeds; false otherwise. If sz is larger than fileName currently is the new bytes will be set to 0, if sz is smaller the file is simply truncated.
| 参数: | sz – PySide.QtCore.qint64 |
|---|---|
| 返回类型: | PySide.QtCore.bool |
Sets the file size (in bytes) sz . Returns true if the file if the resize succeeds; false otherwise. If sz is larger than the file currently is the new bytes will be set to 0, if sz is smaller the file is simply truncated.
另请参阅
PySide.QtCore.QFile.size() PySide.QtCore.QFile.setFileName()
| 参数: | name – unicode |
|---|
设置 name of the file. The name can have no path, a relative path, or an absolute path.
Do not call this function if the file has already been opened.
If the file name has no path or a relative path, the path used will be the application's current directory path at the time of the :meth:`PySide.QtCore.QFile.open` * 调用。
范例:
file = QFile()
QDir.setCurrent("/tmp")
file.setFileName("readme.txt")
QDir.setCurrent("/home")
file.open(QIODevice.ReadOnly) # opens "/home/readme.txt" under Unix
Note that the directory separator “/” works for all operating systems supported by Qt.
| 参数: |
|
|---|---|
| 返回类型: |
PySide.QtCore.bool |
| 参数: | permissionSpec – PySide.QtCore.QFile.Permissions |
|---|---|
| 返回类型: | PySide.QtCore.bool |
| 参数: | fileName – unicode |
|---|---|
| 返回类型: | unicode |
Returns the absolute path of the file or directory referred to by the symlink (or shortcut on Windows) specified by fileName , or returns an empty string if the fileName does not correspond to a symbolic link.
This name may not represent an existing file; it is only a string. QFile.exists() returns true if the symlink points to an existing file.
| 返回类型: | unicode |
|---|
这是重载函数。
Returns the absolute path of the file or directory a symlink (or shortcut on Windows) points to, or a an empty string if the object isn't a symbolic link.
This name may not represent an existing file; it is only a string. QFile.exists() returns true if the symlink points to an existing file.
| 参数: | address – PySide.QtCore.uchar |
|---|---|
| 返回类型: | PySide.QtCore.bool |
取消映射内存 address .
Returns true if the unmap succeeds; false otherwise.
把文件的错误设为 QFile.NoError .