tags:

views:

157

answers:

5

Merry Christmas...

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
A: 

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.

Paul D. Waite
yes I import the correct module...
Lincoln
Weird one. Maybe PyS60 doesn’t like variable names that start with the letter L.
Paul D. Waite
A: 

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)
DNS
Also thought about indentation but there doesn't seem to be a problem.After changing the code to lines = file.readlines() for li in lines L.append(li)still the same error with the error pointing to the word lines in the loop.PS: thanks for pointing it out
Lincoln
Small caveat. That line can be problematic with huge files or for scripts that have performance requirements since readlines() copies all the lines into memory. How about "for line in file"? (
Ross Rogers
The original code is copying all the lines into memory
DNS
By the way, that is a replacement for the entire loop, not just that one line.
DNS
I realize the original code copied into memory. I've used readlines() for scripts that were one-offs or were only occasionally run. I just want the author to understand that it copies the file into memory.
Ross Rogers
A: 

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

Ber
+3  A: 

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)

Lincoln
+1  A: 

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.

Mike DeSimone