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

Joshua Banks syn_ack at comcast.net
Thu Feb 26 10:53:45 EST 2004


----- Original Message ----- 
From: "Blake Winton" <bwinton at latte.ca>
> > 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.

> Just as a side note, you might want to check out
> os.path.join.  You could rewrite your first example as:
> import os, os.path
> dirname = "/var/log/portage"  # Notice the lack of a trailing / !
> a = os.listdir(dirname)
> for x in a:
>     print x, os.path.getsize( os.path.join( dirname, x ) )

> And it might work on my system.
> >>> os.path.join( "var", "log", "portage" )
> 'var\\log\\portage'
>
> (I'm on NT, so my path separators are different from yours.
>  With os.path.join, you don't have to worry about it.  ;)

Thanks for the response Blake.

>>> dirname = os.path.join("c:", "Documents and Settings", "jj", "Desktop")
>>> print dirname
c:Documents and Settings\jj\Desktop

Hmmmm.. This doesn't work like I thought it would... Sorry Blake... But I
don't understand from reading the definition of  "os.path" and your example
of how this is helping in regards to my example? I'll try again below.

I can use either windows or linux for testing.  To recap. In my original
example above I'm forcing python, (atleast I think I am), to know the
ABsolute path name by adding the trailing back-slash(windows) or
forward-slash(linux). This way there is no mistaking this for a file. Me
being anal I guess. Any my reasoning follows:

"/var/log/portage" >>>> "portage" could be a file in parent "log" directory
at first glance.
"/var/log/portage/" >>>> Without a doubt "portage" is a child/subdirectory
of "log".

Here's what I did with windows. Please tell me how "os.path.join" is suppose
to help in regards to the example Im giving? Are you just saying that I
don't have to use the trailing backslashes is all? Just trying to figure out
the added benifits or using "os.path.join". Here's my test results.

import os, os.path
dirname = "c:\\Documents and Settings\\jj\\Desktop"
a = os.listdir(dirname)
for x in a:
    print x, os.path.getsize(dirname + x)

Gives me:
C:\Python23>python dirlist.py
99sl-english
Traceback (most recent call last):
  File "dirlist.py", line 5, in ?
    print x, os.path.getsize(dirname + x)
  File "C:\Python23\lib\ntpath.py", line 228, in getsize
    return os.stat(filename).st_size
OSError: [Errno 2] No such file or directory: 'c:\\Documents and
Settings\\jj\\Desktop99sl-english'

If I try: (using trailing backslashes)
dirname = "c:\\Documents and Settings\\jj\\Desktop\\"
a = os.listdir(dirname)
for x in a:
    print x, os.path.getsize(dirname + x)

This works.

If I try: (without the back slashes[less typing], but [more typing] adding
"os.path.join" below and removing the "+" operator.
dirname = "c:\\Documents and Settings\\jj\\Desktop"
a = os.listdir(dirname)
for x in a:
    print x, os.path.getsize(os.path.join(dirname, x)

This worked as well.

I would like to understand this better because I'm trying to figure out how
to incorparate the use of "os.path.walk" to recursively look through the
specified directory structure to that it lists all subdirectories and
associated files and sizes?

Thanks for you help.

Joshua Banks








More information about the Tutor mailing list