Python / IDLE CPU 无故使用

发布于 2024-11-04 02:05:32 字数 494 浏览 5 评论 0原文

这是我在 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 来执行类似涉及文件数据转换的脚本任务。

Here is a strange problem I have with IDLE (version 2.6.5 with the same Python version) on windows.

I try to run the following three commands:

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

When the print lines command is executed, the pythonw.exe process is going CPU crazy, consuming 100% of CPU and the IDLE seems to not be responding. The file.txt is around 130 kb - I don't consider that file very large !

When the lines finally print (after some minutes), if I try to scroll up to see them, I once again experience the same very large CPU usage.

The memory usage of pythonw.exe is around 15-16 MB all the time.

Can anybody explain to me this behaviour - obviously this can't be a bug in IDLE since it would have been discovered ... Also, what can I do to supress that behavior ? I like using IDLE for script like tasks involving data transformations from files.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最舍不得你 2024-11-11 02:05:32

尝试逐行阅读:

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

for line in fid:
  print line

来自 输入输出,读取文件的方式好像有两种:

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.

Try reading it line by line:

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

for line in fid:
  print line

From the documentation on Input Output, there seem to be two ways to read files:

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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文