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
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
Now, this is QFileInfo
which is wrong!
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
So os.access
is also wrong in one case and returns the same results as QFileInfo
.
I have multiple questions:
- 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.
Edit:
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