Is file object in python an iterable

后端 未结 5 2275
夕颜
夕颜 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:26

    Yes, file objects are iterables but to go back to the beginning of the file you need to use lines.seek(0), since after the first loop you are at the end of the file.

提交回复
热议问题