相关疑难解决方法(0)

使用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万
查看次数

用Python重新打开文件?

说我有这个简单的python脚本:

file = open('C:\\some_text.txt')
print file.readlines()
print file.readlines()
Run Code Online (Sandbox Code Playgroud)

运行时,第一个打印件打印包含文件文本的列表,而第二个打印件打印空白列表.我想这并不完全出乎意料.但有没有办法"回卷"文件,以便我可以再次阅读?或者是重新打开它的最快方式?

python file

34
推荐指数
1
解决办法
4万
查看次数

标签 统计

file ×2

python ×2

iteration ×1