Very stupid question.

Juha-Matti Tapio jmtapio at cs.helsinki.fi
Fri Mar 31 08:51:11 EST 2006


Peter Hansen <peter at engcorp.com> wrote:
>  >>> from path import path
>  >>> path('foobar').getsize()
> 12345L
> (But note that it's just a nice wrapper around the scattered builtin 
> ways of doing the same thing, in this case the os.stat().st_size 
> approach mentioned above.  That's not a bad thing, though, IMHO.)

Also if the file in question is already open, it can be done like this:

os.fstat(fileobject.fileno()).st_size

This form avoids some race condition scenarious with the file being changed 
between stat and open.

I think file sizes should be used carefully and only for visual reasons. 
For code logic it is almost always better to go the "it's easier to ask 
forgiveness than ask permission" -route. Therefore looking up the file size 
is done only rarely and it is not worthy to be a file-object method.

-- 
Juha-Matti Tapio - fil.yo. - +358-50-5419230



More information about the Python-list mailing list