gpt4 book ai didi

Python/IDLE CPU 无故使用

转载 作者:行者123 更新时间:2023-12-01 06:11:11 25 4
gpt4 key购买 nike

这是我在 Windows 上使用 IDLE(版本 2.6.5,具有相同 Python 版本)时遇到的一个奇怪问题。

我尝试运行以下三个命令:

fid= open('file.txt', 'r')
lines=fid.readlines()
print lines

当执行 printlines 命令时,pythonw.exe 进程 CPU 疯狂,消耗 100% CPU 并且 IDLE 似乎没有响应。 file.txt 大约 130 kb - 我不认为该文件很大!

当这些行最终打印出来时(几分钟后),如果我尝试向上滚动查看它们,我会再次遇到同样非常大的 CPU 使用率。

pythonw.exe 的内存使用量始终在 15-16 MB 左右。

任何人都可以向我解释这种行为 - 显然这不可能是 IDLE 中的错误,因为它会被发现......另外,我能做些什么来抑制这种行为?我喜欢使用 IDLE 来执行类似涉及文件数据转换的脚本任务。

最佳答案

尝试逐行阅读:

fid = open('file.txt', 'r')

for line in fid:
print line

来自 Input Output 的文档,读取文件的方式似乎有两种:

print f.read() # This reads the *whole* file. Might be bad to do this for large files.

for l in f: # This reads it line by line
print l # and prints it. Might be better for big files.

关于Python/IDLE CPU 无故使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5820622/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com