finding file size

Sean Ross sross at connectmail.carleton.ca
Fri Jan 2 12:34:53 EST 2004


"Martin v. Loewis" <martin at v.loewis.de> wrote in message
news:bt3l9i$pog$07$3 at news.t-online.com...
> Sean Ross wrote:
>
> > My question is this: Is there a reason why file objects could not have a
> > size method or property?
>
> Yes. In Python, file objects belong to the larger category of "file-like
> objects", and not all file-like objects have the inherent notion of a
> size. E.g. what would you think sys.stdin.size should return (which
> actually is a proper file object - not just file-like)?
>
> Other examples include the things returned from os.popen or socket.socket.
>
> Regards,
> Martin
>

I see what you mean. I suppose the only option I could think of for
sys.stdin, os.popen, and socket.socket would be to return the number of
bytes written to these objects so far. But, then, those objects, or
something else, would have to track that information. Also, pipes and
sockets could be written to from two directions, so is the size the total
number of bytes written from both sides, or would you prefer to know how
much you'd written as the size, or how much the other side had written
(Perhaps all three would be nice). Another option would be to return '-1',
or 'None', to let people know that the request is unsupported for this
file-like object. Still another option would be to raise an exception. And,
of course, there's the ever popular, leave-well-enough-alone option.

Anyway, thank you for your response. I see it's merit.
Sean





More information about the Python-list mailing list