[Tutor] invalid syntax? huh!?!

Jeff Shannon jeff@ccvcorp.com
Fri, 18 Oct 2002 10:49:37 -0700


Ray Leggett wrote:

> File "C:\scripts\modscan.py", line 16
>   for line in fp.readlines()
>   ^
> SyntaxError:  invalid syntax
>
> [....]

> def ScanModule(ModuleToScan):
>  fp = open(ModuleToScan)
>   for line in fp.readlines()
>    if ScanClass.search(line):

[...]

If you get a syntax error at the beginning of a line, then it's often an
indication that there's something wrong with the *previous* line, line
termination, etc.

In this case, you've got an indentation problem.  The 'for ...' should line up
with the 'fp = ...' in the previous line.  There's no change of program flow,
so there should be no change of indentation.  There's another indentation
problem elsewhere in your code, too -- the nested 'if ...' in your 'else...'
clause needs to have the line below it ('print line') indented.  These things
will probably be easier to spot if you use standard 4-space indentation
instead of the single-space indentation that you seem to be using.  (Or is
that just various mailers munging tabs?  Either way, I recommend 4 spaces,
since that's the Pythonlabs standard... :) )

There's another syntax error that'll bite you as soon as you fix this one, too
-- the for statement needs to be terminated with a colon, which you've
accidentally left out.

Jeff Shannon
Technician/Programmer
Credit International