file size 'mystery'

Ilariu Raducan lale at fotonation.com
Tue May 20 10:30:21 EDT 2003


file.read() reads all the file once
a second call to file.read() returns 0 (EOF) because you already read 
everything

Lale

Sven Brandt wrote:
> Hi,
> 
> I'd like to determin the size of a file an do stuff at a certain size 
> (here > 0 byte). In Python:
> 
>   my_file=open(some_file)      # some_file has size of 1000 byte
> 
>   print len(my_file.read())    # prints 1000
>   x=len(my_file.read())
> 
>   if len(my_file.read()) > 0:
>     print len(my_file.read())  # prints 0
> 
>   return printed
> 
> 
> The script returns
> 1000
> 0
> 
> Why is the file size different in the if-clause? (I'm using Python 2.1 
> through Zope 2.6.1).
> 
> Regards
> Sven
> 





More information about the Python-list mailing list