lhcgeneva
lhcgeneva

Reputation: 1981

Python: invalid syntax in function print()

I am using Python 2.7. When I try to print a simple string to a file, I get the following error:

Syntax error: invalid tuple

Syntax error while detecting tuple

minimal example:

fly = open('workfile', 'w')
print('a', file=fly)

writing to the same file via fly.write('a') works just fine.

Upvotes: 4

Views: 6820

Answers (2)

Lennart Regebro
Lennart Regebro

Reputation: 172209

You are using the Python 3 syntax in Python 2.

In Python 2, it's like this:

print >> fly, 'a'

However, a better idea is to do this:

from __future__ import print_function

Which will enable the Python 3 syntax if you are using Python 2.6 or 2.7.

See also: http://docs.python.org/2/library/functions.html#print

Upvotes: 10

couchemar
couchemar

Reputation: 1947

Check the documentation

Note This function is not normally available as a built-in since the name print is recognized as the print statement. To disable the statement and use the print() function, use this future statement at the top of your module: from future import print_function

Upvotes: 0

Related Questions