How to Print to Stderr in Python

How do I print to stderr in Python?

I found this to be the only one short, flexible, portable and readable:

import sys

def eprint(*args, **kwargs):
print(*args, file=sys.stderr, **kwargs)

The optional function eprint saves some repetition. It can be used in the same way as the standard print function:

>>> print("Test")
Test
>>> eprint("Test")
Test
>>> eprint("foo", "bar", "baz", sep="---")
foo---bar---baz

In python, can I redirect the output of print function to stderr?

Do this in your method:

import sys
sys.stdout = sys.stderr

function to write on stderr with python2 and python3

If you are using Python2.7, you can import the new behaviour:

from __future__ import print_function

That should be the first line of code (but could go after a shebang).

Another alternative compatible with earlier versions is to create it in an external module.

if sys.version_info >= (3, 0):
from print_sderr3 import writeStdErr
else:
from print_stderr2 import writeStdErr

where you have implemented each one accordingly.

That is to answer your question, BUT, you can just use sys.stderr.write for both. The only difference is that in Python 3 it seems to return the number of characters written. If you do it on interactive mode:

>>> sys.stderr.write('aaaa\n')
aaaa
5

You get an extra 5, but that is just the return value.

>>> a = sys.stderr.write('aaaa\n')
aaaa
>>> a
5

print vs stderr

They're just two different things. print generally goes to sys.stdout. It's worth knowing the difference between stdin, stdout, and stderr - they all have their uses.

In particular, stdout should be used for normal program output, whereas stderr should be reserved only for error messages (abnormal program execution). There are utilities for splitting these streams, which allows users of your code to differentiate between normal output and errors.

Why does python print version info to stderr?

Python 3.4 was modified to output to stdout, which is the expected behavior. This is listed as a bug with Python here: http://bugs.python.org/issue18338. The comments on the bug report indicate that while stdout is the reasonable choice, it would break backward compatibility. Python 2.7.9 is largely unchanged, because so much relies on it.

Hope that helps!

how to write std.error to file but std.out to console in python?

It is absolutely possible:

import sys
sys.stderr = open("/tmp/errors.txt", "w")

And all following prints to stderr will go to that file.

You should probably be doing this at the shell level rather than at the Python level, though -- it's far more flexible.

How to output a single print statement out of many and redirect the rest to null in python

Perhaps set sys.stdout to /dev/null during the timeit?

Try it online!

if __name__ == "__main__":
from timeit import timeit
import sys

matrix = [[10, 20, 30, 40], [15, 15, 25, 55], [17, 19, 17, 18], [1, 8, 9, 2]]
stdout = sys.stdout
with open('/dev/null', 'w') as sys.stdout:
t = timeit(lambda: method1(4, 4, matrix), number=10000)
sys.stdout = stdout
print(t) # <-- 3


Related Topics



Leave a reply



Submit