Is file object in python an iterable

后端 未结 5 2298
夕颜
夕颜 2020-12-14 09:40

I have a file \"test.txt\":

this is 1st line
this is 2nd line
this is 3rd line

the following code

lines = open(\"test.txt\"         


        
5条回答
  •  一个人的身影
    2020-12-14 10:20

    Your are already at the end of the file. File objects are iterators. Once you iterate over them your are at the final position. Iterating again won't start from the beginning. If you would like to start at line one again, you need to use lines.seek(0).

提交回复
热议问题