What does this code mean: "print >> sys.stderr"

Question:

print >> sys.stderr, "Error in atexit._run_exitfuncs:"

Why print ‘>>’ in front of sys.stderr?

Thanks.

Asked By: zjm1126

||

Answers:

This syntax means writes to a file object (sys.stderr in this case) instead of standard output. [Link]

In Python 3.0, print becomes a function instead of a statement: [Link]

print("Error in atexit._run_exitfuncs:", file=sys.stderr)
Answered By: iamamac

From the Python 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 output.

Answered By: James Thompson
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.