1

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?

Lincoln
  • 744
  • 1
  • 7
  • 20
  • are you being meticulous with white space? is the line with the syntax error indented *exactly* as the previous line? – Ross Rogers Dec 21 '09 at 20:59
  • 1
    also, instead of your while loop, you can do 'for line in file:' – Ross Rogers Dec 21 '09 at 21:00
  • 1
    i cannot find any visible error in your code but do not use while-loop, in this context. if you need a list of lines just use `file.readlines()`. also note that if you need to iterate over a sequence the for loop is what you need (his syntax and semantic is different from C for) – mg. Dec 21 '09 at 21:04
  • for line in file doesnt work cause it's no real file behind it. Not iterable – Lincoln Dec 21 '09 at 21:16

5 Answers5

2

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
  • 744
  • 1
  • 7
  • 20
1

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
  • 41,631
  • 10
  • 72
  • 96
0

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
  • 96,640
  • 56
  • 199
  • 270
0

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
  • 37,249
  • 18
  • 95
  • 132
  • 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 Dec 21 '09 at 21:04
  • 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 Dec 21 '09 at 21:05
  • By the way, that is a replacement for the entire loop, not just that one line. – DNS Dec 21 '09 at 21:10
  • 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 Dec 21 '09 at 21:53
0

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

Ber
  • 40,356
  • 16
  • 72
  • 88