How to list files, folders and all their files under the folders

Alex Martelli aleaxit at yahoo.com
Tue May 22 04:14:51 EDT 2001


"Jennifer Jeng" <jeng at cliffie.nosc.mil> wrote in message
news:29gO6.3040$gM2.334204 at news1.onlynews.com...
> Hi,
> Is there a way to list all files, folders and all the files under those
> folders using python.

os.path.walk() is one way -- there are others, but I think that
one is easiest.  For example:

D:\Python21>python
Python 2.1 (#15, Apr 16 2001, 18:25:49) [MSC 32 bit (Intel)] on win32
Type "copyright", "credits" or "license" for more information.
>>> import os.path
>>> def show(arg, dirname, names):
...     print dirname,names
...
>>> os.path.walk('Lib/xml',show,None)
Lib/xml ['CVS', 'dom', 'parsers', 'sax', '__init__.py']
Lib/xml\CVS []
Lib/xml\dom ['CVS', 'domreg.py', 'minidom.py', 'pulldom.py', '__init__.py']
Lib/xml\dom\CVS []
Lib/xml\parsers ['CVS', 'expat.py', '__init__.py']
Lib/xml\parsers\CVS []
Lib/xml\sax ['CVS', 'expatreader.py', 'handler.py', 'saxutils.py',
'xmlreader.py
', '_exceptions.py', '__init__.py']
Lib/xml\sax\CVS []
>>>


Alex






More information about the Python-list mailing list