1

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?

2 Answers 2

6

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)

Sign up to request clarification or add additional context in comments.

2 Comments

Yeah, see my comment above to Ignacio :( How do I get it to use 3.2 when i know it's installed?
Check /usr/local/bin .. it might be installed there.
4

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

5 Comments

I can confirm what Ignacio is saying. Python 3.1 and 3.2 do NOT give the error, but 2.7 does. Oscar must be running this in Python 2.x as far as I'm concerned.
Whats a command i can put in my .py file to show what version is running? should be 3...
crap yeah 2.6.1 with import sys; print(sys.version). But how do I know, on a Mac, where my 3.2 install is? I KNOW it exists, just not where. My she-bang is: #! /usr/bin/pythonw with just #! its Python 2.5
Err... both produce bad interpreter errors. I installed with the Mac OS X bundle from python.org
Found it! /usr/local/bin/python3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.