Creating a list of files in a directory
Jeff Klassen
nospam!jklassen at biblesociety.ca
Wed Nov 7 12:47:39 EST 2001
Hello,
I am new to Python. As a non-programmer I am encouraged by the level of
'success' I feel I have had, relative to similar learning attempts in other
languages.
I would like to simply do the following:
- read all of files of a particular form (e.g. *.cev) from a particular
directory
- manipulate them with a series of re.sub expressions
- write each file to its own output file with a certain form (e.g. *.out).
I have been able to create the re stuff, and process individual files.
However, I cannot get a for loop to do everything in a directory.
Here is the code I came up with. (I am wanting to create a list of all files
that conform to *.cev)
----------------------------
import dircache
import re
workingdir=dircache.listdir('/jobs/python/learning/')
filespec = re.compile(r'.*?\.cev')
filestoprocess = []
for allfiles in workingdir:
matchedfile=filespec.match(allfiles)
filestoprocess.append(matchedfile.group())
print filestoprocess
----------------------------
When I run this script I am returned:
AttributeError: 'None' object has no attribute 'group'
When I issue these commands in the Python Shell, substituting an actual
value for allfiles such as 'mat.cev', everything works OK. I can execute
filestoprocess.append(matchedfile.group()) and then print filestoprocess,
and I am returned ['mat.cev']. Why does this fail to work inside of the
loop?
Thanks very much for any help.
Jeff
More information about the Python-list
mailing list