[Tutor] optparse

Kent Johnson kent37 at tds.net
Tue Dec 23 22:22:29 CET 2008


On Tue, Dec 23, 2008 at 3:10 PM, Matt Herzog <msh at blisses.org> wrote:
>        for filename in os.listdir(directory):
>            result = re.match(s, filename)
>    print result

You never open and read the files. You are searching for the pattern
in the filename, not in the contents of the file.

If there is no match, result will be None. You need to test it and
print it only if it is not None. (Actually I don't think you want to
print the match object, you want to print the filename and the line of
the file that matches. But one thing at a time...) This test and print
should be inside the "for filename" loop; your print statement is
outside the loop.

Kent


More information about the Tutor mailing list