Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 813
lhcgeneva Avatar asked Dec 26 '22 06:12

lhcgeneva


1 Answers

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

like image 116
Lennart Regebro Avatar answered Dec 30 '22 10:12

Lennart Regebro