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

Mathias Panzenboeck e0427417 at student.tuwien.ac.at
Mon Nov 20 22:10:38 EST 2006


Tom Mountney wrote:
> 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

Missing ']' !!!!
[ ... ] can span over multiple lines, so python reads until it gets this "wrong" if statement. ;)

> 
> #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