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

Tom Mountney mountney at hotmail.com
Mon Nov 20 14:01:15 EST 2006


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