Oscar Godson
Oscar Godson

Reputation: 32776

SyntaxError in print() Python3.2?

I'm getting this error in Python3.2 on a simple print statement. I'm going through some tutorials, and this is exactly the code example is written:

fh = open('lines.txt')
for line in fh.readlines():
   print(line, end="")

But this is what i get

File "./forloop.py", line 5
    print(line, end="")
                   ^
SyntaxError: invalid syntax

Any ideas why?

Upvotes: 1

Views: 2362

Answers (2)

Mark Nenadov
Mark Nenadov

Reputation: 6957

As I said in the comment, I'm quite sure you are on 2.x.

Try this

import sys
print sys.version

If that doesn't give you an error, and says "2.x" then you are certainly. using 2.x

If it gives you a syntax error, its "3.x"

(besides the obvious task of checking what sys.version holds, "print sys.version" is valid 2.x syntax but not valid 3.x syntax)

Upvotes: 6

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799240

That error is generated by Python 2.x. Be sure that you are actually invoking 3.x, whether via command line or via shebang.

Upvotes: 4

Related Questions