Directory listing function

Hi,

Does anybody have a function that I can use that will return a list of the paths of _all_ files on a particular drive? (Windows) Otherwise can somebody give me some guidelines for creating one?

Regards,
Wayne

Wayne Koorts wrote:

Does anybody have a function that I can use that will return a list of
the paths of _all_ files on a particular drive? (Windows) Otherwise can
somebody give me some guidelines for creating one?

you might be able to make use of os.path.walk()

-Chris

···

--
Christopher Barker, Ph.D.
Oceanographer
                                        
NOAA/OR&R/HAZMAT (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov

Chris Barker wrote:

Wayne Koorts wrote:
> Does anybody have a function that I can use that will return a list of
> the paths of _all_ files on a particular drive? (Windows) Otherwise can
> somebody give me some guidelines for creating one?

you might be able to make use of os.path.walk()

I got curious, so I tried it, and it works:

import os

files =

def junk(arg, dirname, names):
    files.extend(names)

os.path.walk("./junk",junk,None)

(you'd want to put "C:" or something rather than "./junk", to do a whole
drive, I just tested it on my junk directory. If your drive is big, this
could take a while!

-CHB

···

--
Christopher Barker, Ph.D.
Oceanographer
                                        
NOAA/OR&R/HAZMAT (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov

Chris Barker wrote:

Chris Barker wrote:
> Wayne Koorts wrote:
> > Does anybody have a function that I can use that will return a list of
> > the paths of _all_ files on a particular drive? (Windows) Otherwise can
> > somebody give me some guidelines for creating one?

I noticed that you wanted the whole path, so here is another version:
#!/usr/bin/env python2.2

import os

files =

def junk(arg, dirname, names):
    for n in names:
        files.append( os.path.join(dirname,n) )

# put the path you want here:
os.path.walk("/home/cbarker/junk",junk,None)

# if you want to print them...
for file in files:
    print file

···

--
Christopher Barker, Ph.D.
Oceanographer
                                        
NOAA/OR&R/HAZMAT (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov