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

John Machin sjmachin at lexicon.net
Mon Nov 20 17:02:47 EST 2006


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




More information about the Python-list mailing list