Creating a list of files in a directory

Prabhu Ramachandran prabhu at aero.iitm.ernet.in
Wed Nov 7 19:14:41 CET 2001


>>>>> "JK" == Jeff Klassen <nospam!jklassen at biblesociety.ca> writes:

    JK> workingdir=dircache.listdir('/jobs/python/learning/') filespec
    JK> = re.compile(r'.*?\.cev') filestoprocess = []

    JK> for allfiles in workingdir:
    JK> matchedfile=filespec.match(allfiles)
    JK> filestoprocess.append(matchedfile.group())

    JK> print filestoprocess ----------------------------

    JK> When I run this script I am returned: AttributeError: 'None'
    JK> object has no attribute 'group'

In which case the match failed.  So add an 

if match:
    filestoprocess.append(...)

Also, you might want to look at the glob module.  It does pretty much
what you want with one command

>>> import glob
>>> glob.glob('/tmp/prabhu/pkg/*.py')
['/tmp/prabhu/pkg/a.py', '/tmp/prabhu/pkg/my_import.py', '/tmp/prabhu/pkg/test.py', '/tmp/prabhu/pkg/__init__.py']

prabhu




More information about the Python-list mailing list