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

Josh Bloom joshbloom at gmail.com
Mon Nov 20 16:01:23 EST 2006


As a first guess what kind of editor are you using to create the .py files?
Maybe MS Word or something that is using smart quotes?

Make sure to use a text editor that doesn't do things like that. Try notepad
and see if that works for you.

-Josh



On 11/20/06, Tom Mountney <mountney at hotmail.com> 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
>
> #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
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20061120/1016f844/attachment.html>


More information about the Python-list mailing list