Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 151295
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T09:25:37+00:00 2026-05-11T09:25:37+00:00

I’ve got a noisy python script that I want to silence by directing its

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T09:25:37+00:00Added an answer on May 11, 2026 at 9:25 am

    You can silence stderr by binding it to a custom writer:

    #!/usr/bin/env python import codecs, sys  class NullWriter:     def write(self, *args, **kwargs):         pass  if len(sys.argv) == 2:    if sys.argv[1] == '1':       sys.stderr = NullWriter()    elif sys.argv[1] == '2':       #NOTE: sys.stderr.encoding is *read-only*        #      therefore the whole stderr should be replaced       # encode all output using 'utf8'       sys.stderr = codecs.getwriter('utf8')(sys.stderr)  print >>sys.stderr, u'\u20AC' # euro sign print 'ok' 

    Example:

    $ python silence_stderr.py Traceback (most recent call last):   File 'silence_stderr.py', line 11, in <module>     print >>sys.stderr, u'\u20AC' UnicodeEncodeError: 'ascii' codec can't encode character u'\u20ac' in position 0: ordinal not in range(128) 

    Silenced stderr:

    $ python silence_stderr.py 1 ok 

    Encoded stderr:

    $ python silence_stderr.py 2 € ok 

    NOTE: I’ve got the above outputs inside emacs therefore to emulate it in a terminal you could do:

    $ python ... 2>out.txt $ cat out.txt 

    NOTE: Inside Windows console (after chcp 65001 that switch to ‘utf-8’ and with truetype font (Lucida Console)) I’ve got strange results:

    C:\> python silence_stderr.py 2 Traceback (most recent call last):   File 'silence_stderr.py', line 14, in <module>     print >>sys.stderr, u'\u20AC' # euro sign   File 'C:\pythonxy\python\lib\codecs.py', line 304, in write     self.stream.write(data) IOError: [Errno 13] Permission denied 

    If the font is not truetype then the exception doesn’t raise but the output is wrong.

    Perl works for the truetype font:

    C:\> perl  -E'say qq(\x{20ac})' Wide character in print at -e line 1. € 

    Redirection works though:

    C:\>python silence_stderr.py 2 2>tmp.log ok C:\>cat tmp.log € cat: write error: Permission denied 

    re comment

    From codecs.getwriter documentation:

    Look up the codec for the given encoding and return its StreamWriter class or factory function. Raises a LookupError in case the encoding cannot be found.

    An oversimplified view:

    class UTF8StreamWriter:     def __init__(self, writer):         self.writer = writer     def write(self, s):         self.writer.write(s.encode('utf-8'))  sys.stderr = UTF8StreamWriter(sys.stderr) 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I want use html5's new tag to play a wav file (currently only supported
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.