Creating a list of files in a directory

Prabhu Ramachandran prabhu at
Wed Nov 7 19:14:41 CET 2001

>>>>> "JK" == Jeff Klassen <nospam!jklassen at> 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(

    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:

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/', '/tmp/prabhu/pkg/', '/tmp/prabhu/pkg/', '/tmp/prabhu/pkg/']


More information about the Python-list mailing list