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

Discussion in 'Python' started by Tom Mountney, Nov 20, 2006.

  1. Tom Mountney

    Tom Mountney Guest

    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
     
    Tom Mountney, Nov 20, 2006
    #1
    1. Advertising

  2. Tom Mountney wrote:

    > 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


    on my machine, I get

    C:\> python grep.py
    File "grep.py", line 29
    (g = dirGrep("c:\\winnt"))
    ^
    SyntaxError: invalid syntax

    which at least points to the right line. the error here is that you
    cannot put parentheses around an assignment in Python; grouping parens
    can only be used in expressions, and assignment isn't an expression in
    Python.

    </F>
     
    Fredrik Lundh, Nov 20, 2006
    #2
    1. Advertising

  3. Tom Mountney

    John Machin Guest

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

    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)


    Some books have websites from which you can download the source of
    examples so you don't have to type them in carefully yourself; is this
    one not so?

    > ----------------------------------------------------------
    >


    [snip]

    > def findfiles(self, pattern):
    > "Accepts pattern, returns filenames that contain pattern"
    > return[x for x in self.files if x.find(pattern) != -1


    You are missing a bracket ("]") off the end of this line. Python allows
    automatic statement continuation while there are excess left
    parentheses brackets & braces, so you don't get told about the error
    until the next line.

    BTW, most modern text editors and IDEs have some sort of assistance
    with ()[]{}

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


    HTH,
    John
     
    John Machin, Nov 20, 2006
    #3
  4. Tom Mountney

    Tim Chase Guest

    > I'm learning python and from a book I tried this program: (all the
    > indentation is there just doen't appear in this EMail)
    > ----------------------------------------------------------
    > if __name__ == "__main__":
    > (g = dirGrep("c:\\winnt"))
    >
    > 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


    I suspect it has to do with the funky (useless? unneeded?
    worthless? broken? borked? crack-smokin'?) parens around the
    non-statement in the subsequent (#29) line.

    The error is a little misleading, as there's nothing wrong with
    line #28 per-se, but rather the following line.

    -tkc
     
    Tim Chase, Nov 20, 2006
    #4
  5. Tom Mountney

    John Machin Guest

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

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

    >
    > on my machine, I get
    >
    > C:\> python grep.py
    > File "grep.py", line 29
    > (g = dirGrep("c:\\winnt"))
    > ^
    > SyntaxError: invalid syntax
    >
    > which at least points to the right line. the error here is that you
    > cannot put parentheses around an assignment in Python; grouping parens
    > can only be used in expressions, and assignment isn't an expression in
    > Python.
    >


    Well it looks like I'm outvoted 1001 to 1 on a weighted basis, but
    according to both Mozilla Thunderbird's news gadget and Google Groups,
    the "return" line in the function immediately before the errored line
    is setting up a list comprehension but there is no trailing "]". The
    code does suffer from having tabs changed to spaces so that the
    effective indentation is *one* space per indentation level, but I can't
    see offhand where that would account for the different effects that are
    being reported. I get the same results with Python 2.4.3 and 2.5
    (Windows distribution).

    The OP is obviously on Windows and *aarrgghh!!* putting his test files
    in his *root* *directory*
    C:\> python grep.py
    and so is Fredrik?? Presumably reproducing exactly the OP's conditions
    :) This is what I did:

    C:\junk>python mountney.py [Note: this is 2.4.3]
    File "mountney.py", line 28
    if __name__ == "__main__":
    ^
    SyntaxError: invalid syntax

    C:\junk>\python25\python mountney.py
    File "mountney.py", line 28
    if __name__ == "__main__":
    ^
    SyntaxError: invalid syntax

    *Shouldn't* make a difference.

    The *only* difference I get from the OP's result is that I get the
    caret exactly under the colon in the "if" line; in the message I'm
    reading in Thunderbird the caret *appears* way out to the right.
    Perhaps the OP could e-mail a copy of the *exact* file (if still
    available) to both Fredrik and myself?

    The code also suffers from typos that will bite the OP as soon as he
    fixes up whatever the current problem is -- e.g. "none", "findfiles"
    instead of "findFiles" -- hence my comment on downloading.

    It also suffers from some deficiencies that can't obviously be blamed
    on the OP's typos:

    E.g. "import os" *and* "from os import isfile" but "os.listdir" is then
    used in the same statement as "isfile"!? And in any case isfile is in
    os.path!!!

    Cheers,
    John
     
    John Machin, Nov 20, 2006
    #5
  6. Tom Mountney

    Tom Mountney Guest

    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" <> wrote in message
    news:...
    > 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
    >
    >
     
    Tom Mountney, Nov 21, 2006
    #6
  7. 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
    >
    >
     
    Mathias Panzenboeck, Nov 21, 2006
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brian Blais

    strange error I can't figure out...

    Brian Blais, Feb 18, 2006, in forum: Python
    Replies:
    3
    Views:
    330
    Brian Beck
    Feb 19, 2006
  2. Replies:
    2
    Views:
    682
  3. Albert Hopkins
    Replies:
    3
    Views:
    472
    Steven D'Aprano
    Dec 10, 2008
  4. laredotornado
    Replies:
    2
    Views:
    130
    Peter Makholm
    Nov 12, 2010
  5. Stefan Mueller
    Replies:
    26
    Views:
    194
    Thomas 'PointedEars' Lahn
    Nov 27, 2005
Loading...

Share This Page