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

Joshua Banks syn_ack at comcast.net
Thu Feb 26 12:10:09 EST 2004

----- Original Message ----- 
From: "Blake Winton" <bwinton at latte.ca>
> It doesn't really help in your example, because you're only going
> one directory deep, but pretend you wanted to grab all the files in
> all the sub-directories of jj...
> > 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?
> That's exactly when you would want to use os.path.join.  Because
> if you have:
> basedir = getHomeDirectory( "jj" )
>   # returns either "C:\\Documents and Settings\\jj\\" or "/home/jj/"
> subdir = "Desktop"
> subfile = "file.txt"

> So now you want to create the path of basedir + subdir + subfile,
> but that's not going to work, because you don't know what separator
> to add between subdir and subfile, because you don't know which OS
> you're running on.  And that's where os.path.join comes in.  You
> can just pass it all the bits, and it will figure it out for you.

> Make sense?

Blake can you provide me with a quick example of using os.walk.path and
os.path.join using my example?
I'm totally lost at this point. I thought that I understood but I'm back to
droolling with a blank stare. I think I need to sign up for a Python course.

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)

Joshua Banks

More information about the Tutor mailing list