我有一个文件“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根本不打印?