【问题标题】:How to reset QFile/ QTextStream?如何重置 QFile/QTextStream?
【发布时间】: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


【解决方案1】:

不知道我应该说什么,但在系统完全重新启动后,“试用二”起作用了。

在第二次重新启动后(两天后进行了大量代码更改),“试用版”也出现了。

毕竟:在我看来,这两个试验都是有效且有效的。

如果您在开发和调试过程中发现异常行为,请尝试重新启动。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-25
    相关资源
    最近更新 更多