4

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.

lhcgeneva
  • 1,981
  • 2
  • 21
  • 29
  • This is essentially the same question, but other way around: [Invalid syntax when using "print"?](http://stackoverflow.com/questions/937491/invalid-syntax-when-using-print) – Lennart Regebro Apr 04 '13 at 13:35

2 Answers2

10

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

Lennart Regebro
  • 167,292
  • 41
  • 224
  • 251
0

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

couchemar
  • 1,927
  • 19
  • 22