[Tutor] Getting "file sizes"

Joshua Banks syn_ack at comcast.net
Wed Feb 18 22:10:49 EST 2004


On Wednesday 18 February 2004 05:29 pm, R. Alan Monroe wrote:
> > Yes, I can get the file size of an individual file by specifying
> > the complete path. But if I have a directory with 60 files then I
> > would have to manually put the path in for each file in that
> > directory that I wish to know the size of.
> > [snip]
> > What I don't understand is how to get Python to use the "getsize()"
> > function for each file that the "os.listdir('/var/log/portage')"
> > function lists.
>
> This isn't recursive, but for a single directory it would work:
>
> [(x,os.path.getsize(x)) for x in os.listdir('c:/')]


:D Ahhhh. A one-liner. I like this. Am I doing something wrong here?

I'm doing this in linux. 
>>> import os, os.path
>>> [(x,os.path.getsize(x)) for x in os.listdir('/var/log/portage')]
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "/usr/lib/python2.3/posixpath.py", line 142, in getsize
    return os.stat(filename).st_size
OSError: [Errno 2] No such file or directory: 
'1782-openmotif-2.1.30-r3.log'

This is the results of using that same line on Windows 2K.

>>> import os, os.path
>>> [(x,os.path.getsize(x)) for x in os.listdir("c:/")]
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "C:\Python23\lib\ntpath.py", line 228, in getsize
    return os.stat(filename).st_size
OSError: [Errno 2] No such file or directory: 'arcldr.exe'


Thanks for the response anyways. I have my homework cut out for me and 
your response and the others have helped point me in the right 
direction.

Thanks,
Joshua Banks




More information about the Tutor mailing list