Simple code but can't find the error (PyS60 but not specific)

1k views Asked by At

I'm a Python beginner and now it's freakin me out:

L = []
file = urllib.urlopen("http://someurl.com/someText.txt")
line = file.readline()
while line != "" :
  L.append(line)
  line = file.readline()
appuifw.selection_list(choices=L)

and I get this error:

 line = file.readline()
 ^
SyntaxError: invalid syntax

Does anyone know what the issue is?

5

There are 5 answers

1
Paul D. Waite On

Seems to work fine in my Python interpreter (2.6.1).

I take it you did do import urllib first? (Not doing that would cause a NameError, not a SyntaxError.)

EDIT: a quick Google found this: http://discussion.forum.nokia.com/forum/showthread.php?t=150563

It’s 18 months old, but it claims that PyS60 is Python 2.2.2. I don’t have that on my machine, but it might be worth seeing if that’s the issue.

4
DNS On

I actually don't see a problem, unless you're mixing tabs and spaces in that indentation, in which case the error should complaining about indentation levels. But I thought I'd point out that there's a much cleaner way to read all the lines in a file-like object:

f = urllib.urlopen("http://someurl.com/someText.txt")
lines = f.readlines()
appuifw.selection_list(choices=lines)
0
Ber On

You are overwriting the built-in function file with you variable of the same name. Maybe that causes the Py60 some grief?

0
Lincoln On

Rewriting to

file = urllib.urlopen("http://blabla.com/bla.txt")
lines1 = file.readlines()
for li in lines1:
  L.append(li)
index = appuifw.selection_list(choices=L)

it seems to work now.
(Still problems left but I think it's the URL)

0
Mike DeSimone On

Show the invisibles. I bet there is an illegal character (null is a favorite) hiding in one of those lines and it's not showing up on your screen. Or maybe the file has the wrong type of line ends.

My usual tricks here:

1) You might have typed it in right in StackOverflow; try copying this code back into the source and see if it fixes things. Sometimes it's hard to see if you put a ] where a ) or } should be.

2) Comment out all the lines, then uncomment them one at a time until the syntax error reappears. If the syntax error is there when you comment out all the other lines, then ther real problem is upstream.

3) Delete the line in question and a couple lines below and above it. Delete these lines in a single operation; you don't want the bad character to stay around because it was in between two lines that you deleted one at a time. Then retype those lines. Don't paste them back in; that might just paste the problem right back in.