I've got a noisy python script that I want to silence by directing its stderr output to /dev/null (using bash BTW).
Like so:
python -u parse.py 1> /tmp/output3.txt 2> /dev/null
but it quickly exits prematurely. Hmm. I can't see the traceback because of course that goes out with stderr. It runs noisily and normally if I don't direct stderr somewhere.
So let's try redirecting it to a file somewhere rather than /dev/null, and take a look at what it's outputting:
python -u parse.py 1> /tmp/output3.txt 2> /tmp/foo || tail /tmp/foo
Traceback (most recent call last):
File "parse.py", line 79, in <module>
parseit('pages-articles.xml')
File "parse.py", line 33, in parseit
print >>sys.stderr, "bad page title", page_title
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)
So, the stderr that's being generated contains utf8, and for some reason python refuses to print non-ascii when it's being redirected, even though it's being directed to /dev/null (though of course python doesn't know that).
How can I silence the stderr of a python script even though it contains utf8? Is there any way to do it without re-writing every print to stderr in this script?
You could also just encode the string as ASCII, replacing unicode characters that don't map. Then you don't have to worry about what kind of terminal you have.
That replaces the characters that can't be encoded with backslash-escapes, i.e.
\xfc
. There are some other replace options too, described here:http://docs.python.org/library/stdtypes.html#str.encode
When stderr is not redirected, it takes on the encoding of your terminal. This all goes out the door when you redirect it though. You'll need to use sys.stderr.isatty() in order to detect if it's redirected and encode appropriately.
You can silence stderr by binding it to a custom writer:
Example:
Silenced stderr:
Encoded stderr:
NOTE: I've got the above outputs inside emacs therefore to emulate it in a terminal you could do:
NOTE: Inside Windows console (after
chcp 65001
that switch to 'utf-8' and with truetype font (Lucida Console
)) I've got strange results:If the font is not truetype then the exception doesn't raise but the output is wrong.
Perl works for the truetype font:
Redirection works though:
re comment
From
codecs.getwriter
documentation:An oversimplified view: