我无法从 QTemporaryFile 创建的临时文件中读取,也许你可以检查下面的代码......
>>> from PyQt4 import QtCore
>>> fileTemp = QtCore.QTemporaryFile()
>>> fileTemp.open()
True #file is created
>>> fileTemp.fileName()
PyQt4.QtCore.QString(u'/tmp/qt_temp.TJ2302')
>>> fileTemp.writeData('foobar')
6L #six bytes are written, great
>>> open(fileTemp.fileName(), 'r').read()
'' #but there is still nothing inside!
>>> fileTemp.size()
6L #magically, by doing this 'foobar' appears in this tempFile...
#the same effect gives e.g. fileTemp.readData(1000), but nothing is read
>>> open(fileTemp.fileName(), 'r').read()
'foobar'
>>> fileTemp.readData(1000)
'' #why...
>>> fileTemp.readAll()
PyQt4.QtCore.QByteArray('')
>>> fileTemp.readAll().size()
0 #why...
将不胜感激任何帮助!
编辑
我已经扩展 QTemporaryFile 以按预期运行,但仍在寻找更干净的解决方案......
class MyTemporaryFile(QtCore.QTemporaryFile):
def __init__(self, *argc, **argv):
QtCore.QTemporaryFile.__init__(self, *argc, **argv)
def write(self, data):
f = open(self.fileName(), 'w')
f.write(data)
f.close()
def read(self):
f = open(self.fileName(), 'r')
data = f.read()
f.close()
return data