我有一个嘈杂的python脚本,我想通过将其stderr输出指向/ dev/null(使用bash BTW)来保持沉默.
像这样:
python -u parse.py 1> /tmp/output3.txt 2> /dev/null
Run Code Online (Sandbox Code Playgroud)
但它很快就会过早退出.嗯.我无法看到追溯因为当然与stderr一起出现了.如果我不指示某个地方的stderr,它会吵闹地运行.
因此,让我们尝试将其重定向到某个地方的文件而不是/ dev/null,并查看它的输出内容:
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)
Run Code Online (Sandbox Code Playgroud)
因此,正在生成的stderr包含utf8,并且由于某种原因,python在被重定向时拒绝打印非ascii,即使它被定向到/ dev/null(当然python当然不知道).
即使它包含utf8,我如何使python脚本的stderr静音?有没有办法在没有重写这个脚本中的每个打印到stderr?