Iterating on a file doesn't work the second time

后端 未结 4 1571
礼貌的吻别
礼貌的吻别 2020-11-22 08:47

I have a problem with iterating on a file. Here\'s what I type on the interpreter and the result:

>>> f = open(\'baby1990.html\', \'rU\')
>>&g         


        
4条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-22 09:26

    The file object is a buffer. When you read from the buffer, that portion that you read is consumed (the read position is shifted forward). When you read through the entire file, the read position is at the end of the file (EOF), so it returns nothing because there is nothing left to read.

    If you have to reset the read position on a file object for some reason, you can do:

    f.seek(0)
    

提交回复
热议问题