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.

1

2 Answers 2

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

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

2 Comments

Python 2 documentation gives the same syntax docs.python.org/2/library/functions.html#print edit: thanks ;)
@lhcgeneva: Read the note in the link you just linked to.
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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.