What does print >> do in python?

What does print >> do in python?

The >> sys.stderr part makes the print statement output to stderr instead of stdout in Python 2.

To quote the documentation:

print also has an extended form, defined by the second portion of the
syntax described above. This form is sometimes referred to as “print
chevron.” In this form, the first expression after the >> must
evaluate to a “file-like” object, specifically an object that has a
write() method as described above. With this extended form, the
subsequent expressions are printed to this file object. If the first
expression evaluates to None, then sys.stdout is used as the file for

In Python 3 use the file argument to the print() function:

 print(spam, file=sys.stderr)

To convert these from Python 2 to Python 3, change:

print >>sys.stderr, Hello


print(Hello, file=sys.stderr)

What does print >> do in python?

For printing to stderr note


is portable across versions, yet you need to add a newline, unlike print; for instance

import sys

errlog = sys.stderr.write
errlog(an error messagen)

Leave a Reply

Your email address will not be published. Required fields are marked *