【问题标题】:Python: suppressing the unsolicited console outputPython:抑制未经请求的控制台输出
【发布时间】:2014-12-10 14:21:26
【问题描述】:

我有一个创建一些控制台输出的应用程序。它还使用了第三方库,这恰好会产生一些视觉垃圾:

25/25   /home/alexey/dir-dst/dir-src1/0025-12 Byzantine Rulers_ Part 17 - Conclusion.mp3
24/25   /home/alexey/dir-dst/dir-src1/0024-12 Byzantine Rulers_ Part 16 - Constantine XI.mp3
 /home/alexey/spaces/python/py-procr/procr/lib/python3.4/site-packages/mutagen/id3/_frames.py:137: ID3Warning: Leftover data: WFED: b'http://www.anders.com/lectures/lars_brownworth/12_byzantine_rulers/rss.xml\x00' (from b'\x00http://www.anders.com/lectures/lars_brownworth/12_byzantine_rulers/rss.xml\x00')
ID3Warning)
23/25   /home/alexey/dir-dst/dir-src1/0023-12 Byzantine Rulers_ Part 15 - Isaac.mp3
22/25   /home/alexey/dir-dst/dir-src1/0022-12 Byzantine Rulers_ Part 14 - Alexius.mp3
21/25   /home/alexey/dir-dst/dir-src1/0021-12 Byzantine Rulers_ Part 13 - Basil II.mp3

第 24 行和第 23 行之间的输出不是我的。有没有办法在不篡改库的源代码的情况下摆脱它?

【问题讨论】:

    标签: python console output


    【解决方案1】:

    您看到的输出是警告。您可以使用warnings module,尤其是warnings.simplefilter(),根据各种过滤条件禁止打印警告。

    没有普遍适用的方法来抑制第三方库的输出。

    【讨论】:

    • 非常感谢,斯文!就这么简单:warnings.resetwarnings() warnings.simplefilter('ignore')
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-07
    相关资源
    最近更新 更多