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

T

Tom Mountney

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
 
F

Fredrik Lundh

Tom said:
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>
 
J

John Machin

Tom said:
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
 
T

Tim Chase

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
 
J

John Machin

Fredrik said:
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
 
T

Tom Mountney

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
 
M

Mathias Panzenboeck

Tom said:
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. ;)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,795
Messages
2,569,644
Members
45,357
Latest member
RuthEsteve

Latest Threads

Top