Finding Directory Structures ??

Hans Nowak hnowak at cuci.nl
Wed Jul 25 08:27:06 EDT 2001


>===== Original Message From pmoscatt at bigpond.net.au =====
>Using python, hows the better way of finding the structure of your
>filesystem.  I thought it may have been Dir() but this brings back
>something completely different.
>
>Basically I want to create a app that shows a Tree with the various
>directories.

If you want an application that shows a directory tree like in, say, Internet 
Explorer, you will probably need a GUI like wxPython or Tkinter to implement 
this. There might be widgets available that already do what you want.

On a lower level, you can get directory and file info from Python modules, 
too:

>>> import os
>>> os.listdir(".")
['DLLs', 'Doc', 'hello.py', 'include', 'INSTALL.LOG', 'Lib', 'libs', 
'LICENSE.txt', 'NEWS.txt', 'py.ico', 'pyc.ico', 'pycon.ico', 'python.bat', 
'python.exe', 'pythonw.exe', 'README.txt', 'tcl', 'Tools', 'UNWISE.EXE', 
'w9xpopen.exe']
>>> import glob
>>> glob.glob("*.py")
['hello.py']
>>> glob.glob("c:/*.*")
['c:/AUTOEXEC.BAT', 'c:/bicw06tumble.gif', 'c:/boot.ini', 'c:/BOOTSECT.DOS', 
'c:/CONFIG.SYS', 'c:/CWBANNER1.gif', 'c:/IO.SYS', 'c:/mpcsetup.log', 
'c:/MSDOS.SYS', 'c:/NTDETECT.COM', 'c:/pagefile.sys', 'c:/PDOXUSRS.NET']

For information on subdirectories and such, you might want to look into 
os.path.walk:

  http://www.python.org/doc/current/lib/module-os.path.html

which traverses a directory tree recursively, and allows you to collect the 
info you need.

HTH,

--Hans Nowak





More information about the Python-list mailing list