finding file size

Sean Ross sross at connectmail.carleton.ca
Fri Jan 2 12:44:28 EST 2004


"David M. Wilson" <dw-google.com at botanicus.net> wrote in message
news:99dce321.0401012246.712dd2fb at posting.google.com...
> 1) Using 'fd' as a name for a file object is a bad idea - you can get
> fds from os.open. If you insist on C-ish names, how about 'fp'
> instead? :)
>

or just f would work ...


> 2) There's nothing to stop the file object from having a size method,
except that file-like objects then have more to implement.

See Martin v. Loewis' post for some other rationale.

>
> How about something like:
>
> py> class SizedFile(file):
> ...     def __len__(self):
> ...             oldpos = self.tell()
> ...             self.seek(0, 2)
> ...             length = self.tell()
> ...             self.seek(oldpos)
> ...             return length
> ...
> py> bleh = SizedFile("/etc/passwd")
> py> len(bleh)
> 1520
> py> len([ x for x in bleh ])
> 33
>
>
>
> As I wrote this I realised it's wrong - size() would be better, since
> the length of the sequence is not the number of bytes. Maybe it is in
> binary mode? Dunno, me sleepy, goodnight..
>
>
> David.

Right. size() is more apt. Also, while I appreciate the effort of
subclassing file, what I was looking for was to have the builtin file (or
file-like) objects expose this operation, not just custom implementations.

Thanks for your response,
Sean





More information about the Python-list mailing list