QFileInfo vs QFile测试文件是否可写 [英] QFileInfo vs QFile to test if a file is writable
问题描述
我正在使用PyQt,在Windows上测试我的应用程序时,我注意到了奇怪的行为(一切都在Linux上可以正常工作).
I'm using PyQt and I noticed strange behavior when testing my application with Windows (everything is working as expected with Linux).
我有一个可以读写的文件,我想通过该应用程序对其进行测试:
I have a file that I can read and write and I want to test it from the app:
>>> from PyQt4.QtCore import QFile, QFileInfo
>>> f1 = QFileInfo("C:\Users\Maxime\Desktop\script.py")
>>> f2 = QFile("C:\Users\Maxime\Desktop\script.py")
>>> f1.isWritable()
True
>>> f2.isWritable()
False
因此,看来QFile
在该测试用例上是错误的.但是,在另一个只读文件上:
So it looks like QFile
is wrong on that test case. But, on another file that is read-only:
>>> f1 = QFileInfo("C:\Program Files (x86)\MySoftware\stuff\script.py")
>>> f2 = QFile("C:\Program Files (x86)\MySoftware\stuff\script.py")
>>> f1.isWritable()
True
>>> f2.isWritable()
False
现在,这是QFileInfo
,这是错误的!
Now, this is QFileInfo
which is wrong!
所以我决定也许应该改用os.access
:
So I decided maybe I should use os.access
instead:
>>> import os
>>> os.access("C:\Users\Maxime\Desktop\script.py")
True
>>> os.access("C:\Program Files (x86)\MySoftware\stuff\script.py")
True
所以os.access
在一种情况下也是错误的,并返回与QFileInfo
相同的结果.
So os.access
is also wrong in one case and returns the same results as QFileInfo
.
我有多个问题:
- 我对Windows不熟悉,是否缺少某些内容?
- 使用Qt,我可以使用
QFileInfo
和QFile
来测试是否可以写入文件.我应该使用一个而不是另一个吗? - 如果这只是Qt(和Python ??)中的错误,我希望有一种解决方法也可以在Linux和Mac OS上使用.
- I am not familiar with Windows, is there something I'm missing?
- Using Qt, I can use
QFileInfo
andQFile
to test if a file can be written. Should I use one instead of the other? - In case that's just a bug in Qt (and Python??), I'd like a workaround that can also work on Linux and Mac OS.
弗兰克(Frank)的一个非常有趣的评论解释说,由于我尚未打开文件,因此QFile :: isWritable()将始终返回False.
A very interesting comment from Frank explained that QFile::isWritable() will always return False since I haven't opened the file.
>>> f = QFile("C:\Users\Maxime\Desktop\script.py")
>>> f.open(QFile.WriteOnly)
True
>>> f.isWritable()
True
>>> f = QFile("C:\Program Files (x86)\MySoftware\stuff\script.py")
>>> f.open(QFile.WriteOnly)
False
>>> f.isWritable()
False
推荐答案
对于检查可写性,使用哪一个无关紧要.
For checking writeability, it shouldn't really matter which one you use.
与QFileInfo的主要区别在于,出于性能原因,它缓存有关目标文件的某些信息.但是,您可以使用刷新方法来重新读取信息,或仅使用 setCaching 完全关闭缓存.
The main difference with QFileInfo is that, for performance reasons, it caches some of the information about the target file. However, you can use the refresh method to re-read the information, or just use setCaching to switch caching off altogether.
此外,如问题注释中所述,如果尚未打开文件,则QFile.isWritable将返回False.这不是错误.该文档明确指出, isWritable 检查 OpenMode .在打开文件之前,该值为零(QIODevice.NotOpen);否则,如果未指定,则默认为QIODevice.ReadWrite.
Also, as noted in the question comments, QFile.isWritable will return False if the file has not been opened. This is not a bug. The documentation makes it clear that isWritable checks the OpenMode of the file. This will be zero (QIODevice.NotOpen) before the file is opened, and otherwise defaults to QIODevice.ReadWrite if unspecified.
唯一需要注意的另一个问题是QFileInfo.isWritable之类的方法特定于当前用户.使用 QFileInfo.permission 获取有关其他用户类别的所有权信息(但请注意有关平台差异的警告).这类似于使用os.access
和os.stat
之间的区别.
The only other issue to be aware of is that methods like QFileInfo.isWritable are specific to the current user. Use QFileInfo.permission for ownership information about other classes of user (but note the warning regarding platform differences). This is analogous to the difference between using os.access
and os.stat
.
最后,这是一个测试可写性的简单脚本:
Finally, here's a simple script that tests writeability:
import os, stat, sip
sip.setapi('QString', 2)
from PyQt4.QtCore import QTemporaryFile, QFile, QFileInfo
tmp = QTemporaryFile()
tmp.setAutoRemove(False)
tmp.open()
tmp.close()
path = tmp.fileName()
info = QFileInfo(path)
print('File: %s' % info.filePath())
print('')
print('Qt Writable: %s' % info.isWritable())
print('Qt Permission: %s' % bool(info.permissions() & QFile.WriteUser))
print('Py Writable: %s' % os.access(path, os.W_OK))
print('Py Permission: %s' % bool(os.stat(path).st_mode & stat.S_IWUSR))
tmp = QFile(path)
tmp.setPermissions(QFile.ReadUser)
print('')
print('Set Permissions: ReadUser')
print('')
info.refresh()
print('Qt Writable: %s' % info.isWritable())
print('Qt Permission: %s' % bool(info.permissions() & QFile.WriteUser))
print('Py Writable: %s' % os.access(path, os.W_OK))
print('Py Permission: %s' % bool(os.stat(path).st_mode & stat.S_IWUSR))
tmp.setPermissions(QFile.WriteUser)
print('')
print('Removed: %s' % tmp.remove())
对于我来说,在Linux和WinXp上,我都得到如下结果:
For me, on both Linux and WinXp, I get results like this:
File: /tmp/qt_temp.TJ1535
Qt Writable: True
Qt Permission: True
Py Writable: True
Py Permission: True
Set Permissions: ReadUser
Qt Writable: False
Qt Permission: False
Py Writable: False
Py Permission: False
Removed: True
这篇关于QFileInfo vs QFile测试文件是否可写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!