Issue
I have to translate a code from python 2 into python 3 and I can't understand what does print >>
do and how should I write it in python 3.
print >> sys.stderr, '--'
print >> sys.stderr, 'entrada1: ', entrada1
print >> sys.stderr, 'entrada2: ', entrada2
print >> sys.stderr, '--'
Solution
The >> sys.stderr
part makes the print
statement output to stderr instead of stdout in Python 2.
To quote the documentation:
>>
must evaluate to a “file-like” object, specifically an object that has awrite()
method as described above. With this extended form, the subsequent expressions are printed to this file object. If the first expression evaluates toNone
, thensys.stdout
is used as the file for output.
In Python 3 use the file
argument to the print()
function:
print("spam", file=sys.stderr)
Answered By - Eugene Yarmash
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.