Help!!! New to Python and getting an error I can't figure out

Tom Mountney mountney at hotmail.com
Tue Nov 21 02:20:20 CET 2006


Thanks to all that gave ideas.
Indeed the error was the missing bracket ("]") off the end of line 25 as
stated by John Machin

And yes the parentheses were also in error as suggested by both Fredrik and
Tim  - I forgot to remove the parens which I tried to see if I got a
different error.

Thanks mucho for all your help - the program now runs!!!!
Tom

"Tom Mountney" <mountney at hotmail.com> wrote in message
news:W6idnfi5a6HhZvzYnZ2dnUVZ_rOdnZ2d at comcast.com...
> Any help is greatly appreciated!!!
>
> I'm learning python and from a book I tried this program: (all the
> indentation is there just doen't appear in this EMail)
> ----------------------------------------------------------
>
> #file grep.py
> import os
> from os import isfile
>
> class dirGrep:
>  def __init__(self, directory):
>   self.files = filter(isfile,
>    [os.path.join(directory, x) for x in os.listdir(directory)])
>
>  def findLines(self, pattern, filelist=none):
>   """Accepts pattern, returns lines that contain pattern.
>   Optional 2nd argument is a filelist to search"""
>   if not filelist:
>    filelist = self.files
>   results = []
>   for file in filelist:
>    fo = open(file)
>    results += [x for x in fo.readlines()
>     if x.find(pattern) != -1]
>    fo.close() #explicit close of the file object
>   return results
>
>  def findfiles(self, pattern):
>   "Accepts pattern, returns filenames that contain pattern"
>   return[x for x in self.files if x.find(pattern) != -1
>
> #test
> if __name__ == "__main__":
>  (g = dirGrep("c:\\winnt"))
>  files = g.findFiles(".py")
>  print g.findLines("java", files)
>
> When I try to run the program - python grep.py - I get the following
error:
>
> C:\> python grep.py
>   File "grep.py", line 28
>     if __name__ == '__main__':
>                                              ^
> SyntaxError: invalid syntax
>
> What am I doing wrong?
> Thanks for any help
>
>





More information about the Python-list mailing list