【发布时间】:2021-08-24 16:02:58
【问题描述】:
第一次读取后,我正在获取要重置的文件。所有的谷歌搜索都没有帮助。
第一次读取文件后如何将文件重置为开头?
试用一:
inFile = QFile( self._pathFileName )
inFile.open(QFile.ReadOnly | QFile.Text)
stream = QTextStream(inFile)
# Count first all lines in the file
self._numLinesRead = 0
self._mumLinesTotal = 0
while not stream.atEnd():
self._mumLinesTotal=+1
stream.readLine();
inFile.seek(0)
stream.seek(0)
pos = stream.pos() # pos is equal to 0 after this line verified with debugging
while( not stream.atEnd() ): # but here it still thinks he's at file end and jumps over
....
试验二:
inFile = QFile( self._pathFileName )
inFile.open(QFile.ReadOnly | QFile.Text)
stream = QTextStream(inFile)
# Count first all lines in the file
self._numLinesRead = 0
self._mumLinesTotal = 0
while not stream.atEnd():
self._mumLinesTotal=+1
stream.readLine();
inFile.close()
del inFile
del stream
inFile = QFile( self._pathFileName )
inFile.open(QFile.ReadOnly | QFile.Text)
stream = QTextStream(inFile)
# everyting has been reset?!
while( not stream.atEnd() ): # Nop it still thinks it is atEnd and jumps over
....
我尝试了在网上找到的所有解决方案。没有什么帮助。我做错了什么?
【问题讨论】:
-
嗯。这很奇怪,尤其是第二种情况。你试过不同的文件吗?这些文本文件是在 Windows 上编写的吗?
标签: python-3.x pyqt5