Is file object in python an iterable

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

    It is not only an iterable, it is an iterator, which is why it can only traverse the file once. You may reset the file cursor with .seek(0) as many have suggested but you should, in most cases, only iterate a file once.

提交回复
热议问题