Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > finding file size

Reply
Thread Tools

finding file size

 
 
Sean Ross
Guest
Posts: n/a
 
      01-02-2004
Hi.

Recently I made a small script to do some file transferring (among other
things). I wanted to monitor the progress of the file transfer, so I needed
to know the size of the files I was transferring. Finding out how to get
this information took some time (reading the manuals - googling did not
prove worthwhile). Anyway, I did eventually figure out how to do it (there
are a few ways, including os.path.getsize(filename)).

My question is this: Is there a reason why file objects could not have a
size method or property? So that you could then just ask the file how big it
is using fd.size or fd.size(). I'm just curious, because, well, it seems to
have obvious utility, and the way to find it is less than obvious (at least,
it was to me).

Thanks,
Sean


 
Reply With Quote
 
 
 
 
David M. Wilson
Guest
Posts: n/a
 
      01-02-2004
"Sean Ross" <(E-Mail Removed)> wrote...

> My question is this: Is there a reason why file objects could not have a
> size method or property? So that you could then just ask the file how big it
> is using fd.size or fd.size(). I'm just curious, because, well, it seems to
> have obvious utility, and the way to find it is less than obvious (at least,
> it was to me).


Hey!

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?

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

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.


>
> Thanks,
> Sean

 
Reply With Quote
 
 
 
 
Martin v. Loewis
Guest
Posts: n/a
 
      01-02-2004
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

 
Reply With Quote
 
Sean Ross
Guest
Posts: n/a
 
      01-02-2004

"Martin v. Loewis" <(E-Mail Removed)> wrote in message
news:bt3l9i$pog$07$(E-Mail Removed)-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


 
Reply With Quote
 
Sean Ross
Guest
Posts: n/a
 
      01-02-2004
"David M. Wilson" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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


 
Reply With Quote
 
Gerrit Holl
Guest
Posts: n/a
 
      01-03-2004
Hi,

I propose to add a "filename" type to Python.

Martin v. Loewis wrote:
> 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)?


A different solution to this problem would be to introduce "filename"
type to Python, a subclass of str. The "name" attribute of file would be of this
type. This type would inherit a lot of os.path stuff: getsize becomes
simpler, more readable, and more object oriented, as do other os.path
functions. I think the alternatives look a lot more prety:

OLD NEW
os.path.realpath(fn) fn.realpath()
os.path.getmtime(fp.name) fp.name.getmtime()
os.path.ismount(os.path.dirname(fp.name)) fp.name.dirname().ismount()

It's more beatiful, simpler, flatter (#3), practical, obvious, easy.

problem: what do do with os.path constants?
solution: make them class attributes
problem: how to handle posixpath, ntpath, macpath?
solution: abstract Path class with NTPath, MacPath, PosixPath sublasses which is the actual type of e.g. fn.name on a certain platform
problem: backwards compatibility
solution: same as string methods
problem: "/dev/null" reads as a Path but is a str
solution: path("/dev/null") is a little more typing for a lot more luxery
problem: what to do with commonprefix?
solution: don't know
problem: what to do with os.path.walk?
solution: use os.walk instead
problem: what to do with sameopenfile?
solution: make it a file method
problem: what to do with join, split?
solution: rename to joinpath, splitpath.

Any comments?

yours,
Gerrit.

--
158. If any one be surprised after his father with his chief wife, who
has borne children, he shall be driven out of his father's house.
-- 1780 BC, Hammurabi, Code of Law
--
Asperger's Syndrome - a personal approach:
http://people.nl.linux.org/~gerrit/english/

 
Reply With Quote
 
Martin v. Loewis
Guest
Posts: n/a
 
      01-03-2004
Gerrit Holl wrote:
> Any comments?


It should be possible to implement that type without modifying
Python proper. It might make a good recipe for the cookbook.

Any volunteers?

Regards,
Martin

 
Reply With Quote
 
Peter Otten
Guest
Posts: n/a
 
      01-03-2004
Gerrit Holl wrote:

> I propose to add a "filename" type to Python.


> A different solution to this problem would be to introduce "filename"
> type to Python, a subclass of str. The "name" attribute of file would be
> of this type. This type would inherit a lot of os.path stuff: getsize
> becomes simpler, more readable, and more object oriented, as do other
> os.path functions. I think the alternatives look a lot more prety:


> OLD NEW
> os.path.realpath(fn) fn.realpath()
> os.path.getmtime(fp.name) fp.name.getmtime()
> os.path.ismount(os.path.dirname(fp.name)) fp.name.dirname().ismount()
>
> It's more beatiful, simpler, flatter (#3), practical, obvious, easy.


You might have a look at

http://mail.python.org/pipermail/pyt...ne/108425.html

http://members.rogers.com/mcfletch/p...ng/filepath.py

has an implementation of your proposal by Mike C. Fletcher. I think both
filename class and os.path functions can peacefully coexist.


Peter



 
Reply With Quote
 
Irmen de Jong
Guest
Posts: n/a
 
      01-03-2004
Gerrit Holl wrote:

> Any comments?


Are you aware of Jason Orendorff's path module?
(haven't tried it myself though)

See this thread: http://tinyurl.com/3gq8r (google link)

--Irmen


 
Reply With Quote
 
Just
Guest
Posts: n/a
 
      01-03-2004
In article <(E-Mail Removed)>,
Gerrit Holl <(E-Mail Removed)> wrote:

> I propose to add a "filename" type to Python.
>

[ ... ]
> A different solution to this problem would be to introduce "filename"
> type to Python, a subclass of str. The "name" attribute of file would be of
> this
> type. This type would inherit a lot of os.path stuff: getsize becomes
> simpler, more readable, and more object oriented, as do other os.path
> functions. I think the alternatives look a lot more prety:
>
> OLD NEW
> os.path.realpath(fn) fn.realpath()
> os.path.getmtime(fp.name) fp.name.getmtime()
> os.path.ismount(os.path.dirname(fp.name)) fp.name.dirname().ismount()
>
> It's more beatiful, simpler, flatter (#3), practical, obvious, easy.


This has been proposed a few times, and even implemented at least once:

http://www.jorendorff.com/articles/python/path/

I'm very much in favor of adding such an object, but I don't like Jason
Orendorff's design all that much. There has been a discussion about it
in the past:

http://groups.google.com/groups?q=g:...n&lr=&ie=UTF-8
&oe=UTF-8&safe=off&selm=mailman.1057651032.22842.python-list%40python.org

Just
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: File Size - Big File Size Keith Thompson C Programming 6 10-03-2009 10:28 PM
Preferred Size, Minimum Size, Size Jason Cavett Java 5 05-25-2008 08:32 AM
Disconnect Between HD File Size & PS's File Size One4All Digital Photography 8 09-12-2007 03:02 AM
mega pixels, file size, image size, and print size - Adobe Evangelists Frank ess Digital Photography 0 11-14-2006 05:08 PM
compare file size with online file size tiewknvc9 Java 6 10-01-2006 09:30 AM



Advertisments