继承者: QTemporaryFile
def
copy
(newName)
def
exists
()
def
link
(newName)
def
moveToTrash
()
def
open
(fd, ioFlags[, handleFlags=QFileDevice.DontCloseHandle])
def
readLink
()
def
remove
()
def
rename
(newName)
def
setFileName
(name)
def
symLinkTarget
()
def
copy
(fileName, newName)
def
decodeName
(localFileName)
def
decodeName
(localFileName)
def
encodeName
(fileName)
def
exists
(fileName)
def
link
(oldname, newName)
def
moveToTrash
(fileName[, pathInTrash=None])
def
permissions
(filename)
def
readLink
(fileName)
def
remove
(fileName)
def
rename
(oldName, newName)
def
resize
(filename, sz)
def
setPermissions
(filename, permissionSpec)
def
symLinkTarget
(fileName)
QFileis an I/O device for reading and writing text and binary files and resources . AQFilemay be used by itself or, more conveniently, with aQTextStreamorQDataStream.The file name is usually passed in the constructor, but it can be set at any time using
setFileName().QFileexpects 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
exists(), and remove a file usingremove(). (More advanced file system related operations are provided byQFileInfoandQDir.)The file is opened with
open(), closed withclose(), and flushed withflush(). Data is usually read and written usingQDataStreamorQTextStream, but you can also call theQIODevice-inherited functionsread(),readLine(),readAll(),write().QFilealso inheritsgetChar(),putChar(),和ungetChar(), which work one character at a time.The size of the file is returned by
size(). You can get the current file position usingpos(), or move to a new file position usingseek(). If you’ve reached the end of the file,atEnd()返回true.
The following example reads a text file line by line:
QFile file("in.txt"); if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) return; while (!file.atEnd()) { QByteArray line = file.readLine(); process_line(line); }
文本flag passed toopen()tells Qt to convert Windows-style line terminators (“\r\n”) into C++-style terminators (“\n”). By default,QFileassumes binary, i.e. it doesn’t perform any conversion on the bytes stored in the file.
The next example uses
QTextStreamto read a text file line by line:QFile file("in.txt"); if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) return; QTextStream in(&file); while (!in.atEnd()) { QString line = in.readLine(); process_line(line); }
QTextStreamtakes care of converting the 8-bit data stored on disk into a 16-bit UnicodeQString. By default, it assumes that the user system’s local 8-bit encoding is used (e.g., UTF-8 on most unix based operating systems; seecodecForLocale()for details). This can be changed usingsetCodec().To write text, we can use operator<<(), which is overloaded to take a
QTextStreamon the left and various data types (includingQString) on the right:QFile file("out.txt"); if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) return; QTextStream out(&file); out << "The magic number is: " << 49 << "\n";
QDataStreamis 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
QFile,QFileInfo,和QDirto 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 ofQFile, you can use theencodeName()anddecodeName()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 whichsize()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 callingread(). In this case, however, you cannot useatEnd()to determine if there is more data to read (sinceatEnd()will return true for a file that claims to have size 0). Instead, you should either callreadAll(), or callread()orreadLine()repeatedly until no more data can be read. The next example usesQTextStreamto read/proc/modulesline by line:QFile file("/proc/modules"); if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) return; QTextStream in(&file); QString line = in.readLine(); while (!line.isNull()) { process_line(line); line = in.readLine(); }
不像其它
QIODevice实现,譬如QTcpSocket,QFiledoes not emit theaboutToClose(),bytesWritten(),或readyRead()signals. This implementation detail means thatQFileis not suitable for reading and writing certain types of files, such as device files on Unix platforms.
文件权限的处理是不同的,在像 Unix 系统和 Windows。在非
writabledirectory on Unix-like systems, 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.Qt’s understanding of file permissions is limited, which affects especially the
setPermissions()function. On Windows, Qt will set only the legacy read-only flag, and that only when none of the Write* flags are passed. Qt does not manipulate access control lists (ACLs), which makes this function mostly useless for NTFS volumes. It may still be of use for USB sticks that use VFAT file systems. POSIX ACLs are not manipulated, either.
QFile
¶
QFile(parent)
QFile(name)
QFile(name, parent)
- param parent
- param name
unicode
构造
QFile
对象。
构造新文件对象采用给定
parent
.
PySide2.QtCore.QFile.
copy
(
fileName
,
newName
)
¶
fileName – unicode
newName – unicode
bool
这是重载函数。
拷贝文件
fileName
to
newName
。返回
true
若成功;否则返回
false
.
若文件采用名称
newName
already exists,
copy()
返回
false
(即,
QFile
不会覆写它)。
另请参阅
PySide2.QtCore.QFile.
copy
(
newName
)
¶
newName – unicode
bool
拷贝目前指定的文件通过
fileName()
to a file called
newName
。返回
true
若成功;否则返回
false
.
注意:若文件采用名称
newName
already exists, returns
false
(i.e.
QFile
不会覆写它)。
关闭源文件在拷贝它之前。
另请参阅
PySide2.QtCore.QFile.
decodeName
(
localFileName
)
¶
localFileName
–
QByteArray
unicode
PySide2.QtCore.QFile.
decodeName
(
localFileName
)
¶
localFileName – str
unicode
这是重载函数。
返回 Unicode 版本为给定
localFileName
。见
encodeName()
了解细节。
PySide2.QtCore.QFile.
encodeName
(
fileName
)
¶
fileName – unicode
转换
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.
另请参阅
PySide2.QtCore.QFile.
exists
(
)
¶
bool
这是重载函数。
返回
true
若文件指定通过
fileName()
存在;否则返回
false
.
另请参阅
fileName()
setFileName()
PySide2.QtCore.QFile.
exists
(
fileName
)
¶
fileName – unicode
bool
返回
true
若文件指定通过
fileName
存在;否则返回
false
.
注意
若
fileName
是指向不存在文件的符号链接,返回 false。
PySide2.QtCore.QFile.
link
(
newName
)
¶
newName – unicode
bool
Creates a link named
linkName
that points to the file currently specified by
fileName()
. What a link is depends on the underlying filesystem (be it a shortcut on Windows or a symbolic link on Unix). Returns
true
若成功;否则返回
false
.
This function will not overwrite an already existing entity in the file system; in this case,
link()
will return false and set
error()
to return
RenameError
.
注意
To create a valid link on Windows,
linkName
must have a
.lnk
file extension.
另请参阅
PySide2.QtCore.QFile.
link
(
oldname
,
newName
)
¶
oldname – unicode
newName – unicode
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
若成功;否则返回
false
.
另请参阅
PySide2.QtCore.QFile.
moveToTrash
(
)
¶
bool
移动指定文件按
fileName()
to the trash. Returns
true
若成功,并设置
fileName()
to the path at which the file can be found within the trash; otherwise returns
false
.
注意
On systems where the system API doesn’t report the location of the file in the trash,
fileName()
will be set to the null string once the file has been moved. On systems that don’t have a trash can, this function always returns false.
PySide2.QtCore.QFile.
moveToTrash
(
fileName
[
,
pathInTrash=None
]
)
¶
fileName – unicode
pathInTrash – unicode
bool
这是重载函数。
移动指定文件按
fileName()
to the trash. Returns
true
if successful, and sets
pathInTrash
(if provided) to the path at which the file can be found within the trash; otherwise returns
false
.
注意
On systems where the system API doesn’t report the path of the file in the trash,
pathInTrash
will be set to the null string once the file has been moved. On systems that don’t have a trash can, this function always returns false.
PySide2.QtCore.QFile.
open
(
fd
,
ioFlags
[
,
handleFlags=QFileDevice.DontCloseHandle
]
)
¶
fd
–
int
ioFlags
–
OpenMode
handleFlags
–
FileHandleFlags
bool
这是重载函数。
Opens the existing file descriptor
fd
以给定
mode
.
handleFlags
may be used to specify additional options. Returns
true
若成功;否则返回
false
.
当
QFile
is opened using this function, behaviour of
close()
is controlled by the
AutoCloseHandle
flag. If
AutoCloseHandle
is specified, and this function succeeds, then calling
close()
closes the adopted handle. Otherwise,
close()
does not actually close the file, but only flushes it.
QFile
that is opened using this function is automatically set to be in raw mode; this means that the file input/output functions are slow. If you run into performance issues, you should try to use one of the other open functions.
警告
若
fd
is not a regular file, e.g, it is 0 (
stdin
), 1 (
stdout
), or 2 (
stderr
), you may not be able to
seek()
. In those cases,
size()
返回
0
。见
isSequential()
了解更多信息。
警告
Since this function opens the file without specifying the file name, you cannot use this
QFile
采用
QFileInfo
.
另请参阅
close()
PySide2.QtCore.QFile.
permissions
(
filename
)
¶
filename – unicode
Permissions
这是重载函数。
Returns the complete OR-ed together combination of
Permission
for
fileName
.
PySide2.QtCore.QFile.
readLink
(
)
¶
unicode
注意
此函数被弃用。
使用
symLinkTarget()
代替。
PySide2.QtCore.QFile.
readLink
(
fileName
)
¶
fileName – unicode
unicode
注意
此函数被弃用。
使用
symLinkTarget()
代替。
PySide2.QtCore.QFile.
remove
(
)
¶
bool
Removes the file specified by
fileName()
。返回
true
若成功;否则返回
false
.
The file is closed before it is removed.
另请参阅
PySide2.QtCore.QFile.
remove
(
fileName
)
¶
fileName – unicode
bool
这是重载函数。
Removes the file specified by the
fileName
给定。
返回
true
若成功;否则返回
false
.
另请参阅
PySide2.QtCore.QFile.
rename
(
newName
)
¶
newName – unicode
bool
Renames the file currently specified by
fileName()
to
newName
。返回
true
若成功;否则返回
false
.
若文件采用名称
newName
already exists, returns
false
(即,
QFile
不会覆写它)。
The file is closed before it is renamed.
If the rename operation fails, Qt will attempt to copy this file’s contents to
newName
, and then remove this file, keeping only
newName
. If that copy operation fails or this file can’t be removed, the destination file
newName
is removed to restore the old state.
另请参阅
PySide2.QtCore.QFile.
rename
(
oldName
,
newName
)
¶
oldName – unicode
newName – unicode
bool
这是重载函数。
Renames the file
oldName
to
newName
。返回
true
若成功;否则返回
false
.
若文件采用名称
newName
already exists,
rename()
返回
false
(即,
QFile
不会覆写它)。
另请参阅
PySide2.QtCore.QFile.
resize
(
filename
,
sz
)
¶
filename – unicode
sz
–
qint64
bool
这是重载函数。
集
fileName
to size (in bytes)
sz
。返回
true
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.
警告
This function can fail if the file doesn’t exist.
另请参阅
PySide2.QtCore.QFile.
setFileName
(
name
)
¶
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
open()
** call.
范例:
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.
PySide2.QtCore.QFile.
setPermissions
(
filename
,
permissionSpec
)
¶
filename – unicode
permissionSpec
–
Permissions
bool
这是重载函数。
设置权限为
fileName
file to
permissions
.
PySide2.QtCore.QFile.
symLinkTarget
(
)
¶
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.
exists()
返回
true
if the symlink points to an existing file.
另请参阅
fileName()
setFileName()
PySide2.QtCore.QFile.
symLinkTarget
(
fileName
)
¶
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.
exists()
返回
true
if the symlink points to an existing file.