Writing to standard error (stderr)



examples/sys/stderr.py
import sys

print("on stdout (Standard Output)")
print("on stderr (Standard Error)", file=sys.stderr)
sys.stderr.write("on stderr using write\n")


# x = 0
# print(1/x)

Redirection (Works on Linux/Mac/Windows):


python stderr.py > out.txt  2> err.txt
python stderr.py > all.txt 2>&1

python stderr.py 2> /dev/null            # On Linux and OSX
python stderr.py 2> nul                  # On Windows