欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程语言 > python >内容正文

python

python中eof啥意思,什么是Python的完美对应“而不是EOF”

发布时间:2025/3/19 python 58 豆豆
生活随笔 收集整理的这篇文章主要介绍了 python中eof啥意思,什么是Python的完美对应“而不是EOF” 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

To read some text file, in C or Pascal, I always use the following snippets to read the data until EOF:

while not eof do begin

readline(a);

do_something;

end;

Thus, I wonder how can I do this simple and fast in Python?

解决方案

Loop over the file to read lines:

with open('somefile') as openfileobject:

for line in openfileobject:

do_something()

File objects are iterable and yield lines until EOF. Using the file object as an iterable uses a buffer to ensure performant reads.

You can do the same with the stdin (no need to use raw_input():

import sys

for line in sys.stdin:

do_something()

To complete the picture, binary reads can be done with:

from functools import partial

with open('somefile', 'rb') as openfileobject:

for chunk in iter(partial(openfileobject.read, 1024), ''):

do_something()

where chunk will contain up to 1024 bytes at a time from the file.

总结

以上是生活随笔为你收集整理的python中eof啥意思,什么是Python的完美对应“而不是EOF”的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。