Methods on file-like objects can only used once on one object?

Grant Edwards invalid at invalid.invalid
Tue Aug 23 17:35:56 CEST 2011


On 2011-08-23, Steven D'Aprano <steve+comp.lang.python at pearwood.info> wrote:
> Yingjie Lin wrote:
>
>> Hi Python users,
>> 
>> I just realize that my post yesterday shouldn't be specifically for
>> mechanize. It should be a general question for file-like objects.
>> 
>>>>> f = open('my_file.txt')
>>>>> print f.readlines()
>> ( prints a list of strings
>>>>> print f.readlines()
>> []
>
> Once you've read the file, the file pointer is now at the end of the file.
> To go back to the beginning of the file and read it again, you have to use
> the seek method:
>
> f.seek(0)

It's too bad Python doesn't support the f.rewind() spelling for that
operation.  Rewinding disk files always made me smile...

-- 
Grant Edwards               grant.b.edwards        Yow! Did I say I was
                                  at               a sardine?  Or a bus???
                              gmail.com            



More information about the Python-list mailing list