get all paths

Philip Swartzleonard starx at pacbell.net
Wed Feb 6 15:51:16 EST 2002


Ed || Wed 06 Feb 2002 12:19:13p:

> Hi
> I'm trying to create a treeview widget, but i am having problems
> getting alll directories and files from a specified directory
> 
> i.e
> 
> getList("C:/") should return all directories and files from C:/
> onwards whereas
> getList("C:/Python") should return all from that onwards
> 
> any help would be appreciated

Well, for web site maintance, i've written a few fuctions that recruse 
over a tree and apply other scripts to files with certain extentions. 
(i.e. turn .sxhtml into .html by combining with a .msxhtml template :), 
and this is about how i've gone about doing it. It's probably not the 
best way, but you can probably change it to do what you need.

----
import os

def getList( path, ):

    list = []
    os.chdir( path )
    dir = os.listdir( '.' )
    our_dir = os.path.abspath('.')

    for name in dir:
        list += [os.path.abspath(name)]
        if os.path.isdir(name):
            list += getList(os.path.abspath(name))
            os.chdir( our_dir )

    return list
----

and then this bit of code

x = getList ('c:/desk/sproj')
x.sort()
for i in x:
    print i

produces
c:\desk\sproj\MANIFEST
c:\desk\sproj\calls.stats.sx
c:\desk\sproj\error.sx
c:\desk\sproj\gfx
c:\desk\sproj\gfx\default.png
c:\desk\sproj\gfx\tile
c:\desk\sproj\gfx\tile\floor.png
c:\desk\sproj\gfx\tile\wall.png
c:\desk\sproj\gfx\tile\wall2.png
c:\desk\sproj\globals.py
c:\desk\sproj\globals.pyc
c:\desk\sproj\nameparts.sx
c:\desk\sproj\notes.sx
c:\desk\sproj\profile.temp.dat
c:\desk\sproj\setup.py
c:\desk\sproj\sproject.py
c:\desk\sproj\stats.1.sx
c:\desk\sproj\stats.2.sx
c:\desk\sproj\stats_examine.py
c:\desk\sproj\std.stats.sx
c:\desk\sproj\sxrandom.py
c:\desk\sproj\tdrep.py
c:\desk\sproj\tdrep.pyc
c:\desk\sproj\test.py
c:\desk\sproj\tile.py
c:\desk\sproj\tile.pyc
c:\desk\sproj\todo.sx

-- 
Philip Sw "Starweaver" [rasx] :: www.rubydragon.com



More information about the Python-list mailing list