How do I print to stderr in Python?

I found this to be the only one short, flexible, portable and readable: # This line only if you still care about Python2 from __future__ import print_function 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: … Read more

Bash script – store stderr in a variable [duplicate]

Try redirecting stderr to stdout and using $() to capture that. In other words: VAR=$((your-command-including-redirect) 2>&1) Since your command redirects stdout somewhere, it shouldn’t interfere with stderr. There might be a cleaner way to write it, but that should work. Edit: This really does work. I’ve tested it: #!/bin/bash BLAH=$(( ( echo out >&1 echo … Read more

Piping both stdout and stderr in bash?

(Note that &>>file appends to a file while &> would redirect and overwrite a previously existing file.) To combine stdout and stderr you would redirect the latter to the former using 2>&1. This redirects stderr (file descriptor 2) to stdout (file descriptor 1), e.g.: $ { echo “stdout”; echo “stderr” 1>&2; } | grep -v … Read more

c popen won’t catch stderr

popen gives you a file handle on a process’ stdout, not its stderr. Its first argument is interpreted as a shell command, so you can do redirections in it: FILE *p = popen(“prog 2>&1”, “r”); or, if you don’t want the stdout at all, FILE *p = popen(“prog 2>&1 >/dev/null”, “r”); (Any other file besides … Read more

How to redirect stderr in Python?

I have a piece of software I wrote for work that captures stderr to a file like so: import sys sys.stderr = open(‘C:\\err.txt’, ‘w’) so it’s definitely possible. I believe your problem is that you are creating two instances of writer. Maybe something more like: import sys class writer(object): log = [] def write(self, data): … Read more

How do I write to standard error in PowerShell?

Use Write-Error to write to stderr. To redirect stderr to file use: Write-Error “oops” 2> /temp/err.msg or exe_that_writes_to_stderr.exe bogus_arg 2> /temp/err.msg Note that PowerShell writes errors as error records. If you want to avoid the verbose output of the error records, you could write out the error info yourself like so: PS> Write-Error “oops” -ev … Read more

redirect stdout/stderr to a string

Yes, you can redirect it to an std::stringstream: std::stringstream buffer; std::streambuf * old = std::cout.rdbuf(buffer.rdbuf()); std::cout << “Bla” << std::endl; std::string text = buffer.str(); // text will now contain “Bla\n” You can use a simple guard class to make sure the buffer is always reset: struct cout_redirect { cout_redirect( std::streambuf * new_buffer ) : old( … Read more

Python read from subprocess stdout and stderr separately while preserving order

The code in your question may deadlock if the child process produces enough output on stderr (~100KB on my Linux machine). There is a communicate() method that allows to read from both stdout and stderr separately: from subprocess import Popen, PIPE process = Popen(command, stdout=PIPE, stderr=PIPE) output, err = process.communicate() If you need to read … Read more