【发布时间】:2013-06-04 09:03:43
【问题描述】:
我有一个文件“test.txt”:
this is 1st line
this is 2nd line
this is 3rd line
以下代码
lines = open("test.txt", 'r')
for line in lines:
print "loop 1:"+line
for line in lines:
print "loop 2:"+line
仅打印:
loop 1:this is 1st line
loop 1:this is 2nd line
loop 1:this is 3rd line
它根本不打印loop2。
两个问题:
open() 返回的文件对象,它是可迭代的吗?这就是为什么它可以在 for 循环中使用?
为什么 loop2 根本不打印?
【问题讨论】: