小编MYZ*_*MYZ的帖子

使用Python迭代文件

我有一个问题要理解迭代文件,在这里我继续我在解释器上输入的内容和结果:

>>> f = open('baby1990.html', 'rU')
>>> for line in f.readlines():
...  print(line)
... 
# ... all the lines from the file appear here ...
Run Code Online (Sandbox Code Playgroud)

当我再次尝试迭代同一个打开的文件时,我什么也没得到!

>>> for line in f.readlines():
...  print(line)
... 
>>>
Run Code Online (Sandbox Code Playgroud)

根本没有输出,为了解决这个问题,我要关闭()文件,然后再打开它进行阅读!! 这是正常的行为吗?

python iteration file

51
推荐指数
3
解决办法
11万
查看次数

标签 统计

file ×1

iteration ×1

python ×1