PyQt:QTemporaryFile - 无法从文件中读取

时间:2013-06-02 09:57:59

标签: python pyqt pyqt4 temporary temporary-files

我无法读取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

2 个答案:

答案 0 :(得分:0)

如果您想强制操作系统将数据写入文件,您应始终flush()

In [10]: from PyQt4 import QtCore

In [11]: fileTemp = QtCore.QTemporaryFile()

In [12]: fileTemp.open()
Out[12]: True

In [13]: fileTemp.fileName()
Out[13]: PyQt4.QtCore.QString(u'/tmp/qt_temp.XM5599')

In [14]: fileTemp.writeData('foobar')
Out[14]: 6L

In [15]: with open(fileTemp.fileName(), 'r') as f:
   ....:     print(f.read())
   ....:     


In [16]: fileTemp.flush()
Out[16]: True

In [17]: with open(fileTemp.fileName(), 'r') as f:
    print(f.read())
   ....:     
foobar

size()的调用可能会隐式强制操作系统刷新,因此你观察到的行为。

答案 1 :(得分:0)

所以最终要做到这一点的正确方法是:

>>> from PyQt4 import QtCore
>>> f = QtCore.QTemporaryFile()
>>> f.open()
True
>>> f.writeData('foobar')
6L
>>> f.flush()
True
>>> f.seek(0)
True
>>> f.readAll()
PyQt4.QtCore.QByteArray('foobar')

但它仅适用于Linux ...在Windows上我必须做这样的事情:

>>> ================================ RESTART ================================
>>> from PyQt4 import QtCore
>>> f = QtCore.QTemporaryFile()
>>> f.open()
True
>>> f.writeData('foobar')
6L
>>> f.flush()
True
>>> f.seek(0)
True
>>> f.readAll()
PyQt4.QtCore.QByteArray()
>>> ================================ RESTART ================================
>>> from PyQt4 import QtCore
>>> f = QtCore.QTemporaryFile()
>>> f.open()
True
>>> f.writeData('foobar')
6L
>>> f.flush()
True
>>> f.seek(1)
True
>>> f.seek(0)
True
>>> f.readAll()
PyQt4.QtCore.QByteArray('foobar')

可能会更清楚:

from PyQt4 import QtCore

class MyTemporaryFile(QtCore.QTemporaryFile):
    def __init__(self, *argc, **argv):
        QtCore.QTemporaryFile.__init__(self, *argc, **argv)
        self.open()

    def write(self, data):
        self.writeData(data)
        self.flush()

    def read(self):
        #self.seek(1)   #on windows it works only with this line uncommented
        self.seek(0)
        return self.readAll()