[Tutor] Re: Getting "file sizes" !Sovled!! For real.. Heh..Heh..

Joshua Banks syn_ack at comcast.net
Thu Feb 26 00:45:24 EST 2004


Ok, now I've actually figured out what was really happening. I was
temporarily tricked into thinking I had solved the problem. The
os.path.getsize() function has no idea about the path that is the value of
"a". But it is smart enough to look in the current working directory. So If
I ran the script below in same directory that I was actually searching for
in the script it would run fine.

So the I needed a way to give os.path.getsize() the correct path to look in
so that I could run from a directory other than the one parsed in the
script.

Here its failing:
>>> import os, os.path
>>> a = os.listdir('/var/log/portage')
>>> for x in a:
...     print x, os.path.getsize(x)
...
1782-openmotif-2.1.30-r3.log
Traceback (most recent call last):
  File "<stdin>", line 2, 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'

Here its finally working correctly.

import os, os.path

dirname = "/var/log/portage/"

a = os.listdir(dirname)

for x in a:

print x, os.path.getsize(dirname + x)



Just had to clarify.

Thanks to my brother for helping with this hair bail. Programming is no
joke. It's about the most formal and anal language that I think I'll ever
come acrossed.

Joshua Banks




More information about the Tutor mailing list