Leopoldo
Leopoldo

Reputation: 825

Why when I write to stderr I see STDERR output on the stdout?

import sys

if __name__ == '__main__':
    sys.stdout.write("STDOUT\n")
    sys.stderr.write("STDERR\n")

Could you explain why when I write to stderr I see STDERR output on the stdout? I supposed only STDOUT should be visible in the terminal.

$ python stdout_stdin.py
STDOUT
STDERR

Upvotes: 0

Views: 274

Answers (4)

rjmunro
rjmunro

Reputation: 28056

Try redirecting the output to a file to see how it works:

$ python stdout_stdin.py > output
STDERR
$ cat output
STDOUT

Unless you redirect one of them, by default, both are shown in a normal terminal. STDERR is great for error messages because they show up even when you are saving the actual output to a file.

Upvotes: 0

skyking
skyking

Reputation: 14390

Normally stdout and stderr are displayed in the terminal. If your OS supports it you can redirect one or both to elsewhere. For example you could redirect stdout and you would only see stderr in the terminal (or you could redirect stderr and you would only see stdout in the terminal).

Upvotes: 1

Mangu Singh Rajpurohit
Mangu Singh Rajpurohit

Reputation: 11410

Both stdout and stderr streams are connected with your display device, by default, that's why you are seeing syserr messages on the screen also.

Upvotes: 1

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798436

I supposed only STDOUT should be visible in the terminal.

Incorrect. stderr is a separate file descriptor, but it's still connected to the same tty as stdout by default.

Upvotes: 3

Related Questions