I have a question about the stderr output

Asked 2 years ago, Updated 2 years ago, 162 views

What's the best way I know? It doesn't look like Python after writing it.

I'd appreciate it if you could let me know if there's another way

import sys

#1.
print >> sys.stderr, 'spam'

#2.
sys.stderr.write('spam\n')

#3.
from __future__ import print_function
print('spam', file=sys.stderr)

python printing stderr zen

2022-09-22 22:34

1 Answers

Among the methods you wrote, considering the readability and Python version, I think the second method is the best. The first method is not supported by python3. The third method is also using the __future___ module.

Here's how I came up with it.

from __future__ import print_function
def warning(*objs):
    print("WARNING: ", *objs, file=sys.stderr)


2022-09-22 22:34

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.