在 Hachoir 中抑制警告

Suppress warnings in Hachoir

我正在使用 hachior-parser to grab the duration of a large set of video files. (I'm resetting the "Last modified" date based on the file's timestamp, plus its duration.) I'm using code adapted from this question

我遇到的问题 运行 是 hach​​ior 为每个文件报告四个警告,这使我的输出变得混乱。我仍然从文件中获取我的持续时间,所以我想知道如何在可能的情况下在输出中抑制这些警告。

Python 并不是我的强项,所以我不确定去哪里查看,而且关于错误报告的 hach​​ior 文档似乎很少。我不想求助于从我的脚本输出中获取行。

编辑: 运行 python -W ignore set_last_modified.py 导致打印相同的 [warn] 行。

[warn] [/headers/stream[2]/stream_fmt] Can't get field "stream_hdr" from /headers/stream[2]
[warn] [/headers/stream[2]/stream_fmt] [Autofix] Fix parser error: stop parser, add padding
[warn] [/headers/stream[3]/stream_fmt] Can't get field "stream_hdr" from /headers/stream[3]
[warn] [/headers/stream[3]/stream_fmt] [Autofix] Fix parser error: stop parser, add padding

您可以使用 -W 选项来抑制 python 中的警告。

python -W ignore my_file.py

编辑:既然您已经尝试过上述方法,那么您可以尝试以下方法。

import warnings
# add the following before you call the function that gives warnings.
warnings.filterwarnings("ignore")
# run your function here

我通过检查 BitBucket 上项目的问题页面找到了解决方案。

https://bitbucket.org/haypo/hachoir/issues/54/control-log-level-whith-the-python-api

from hachoir_core import config as HachoirConfig
HachoirConfig.quiet = True