如何抑制subprocess.call中的输出,但保存输出?

时间:2013-10-03 21:52:45

标签: python process mediainfo

目前我的代码是

print ("Getting MediaInfo...")
mediainfo_command = ("mediainfo", input_file_full)
mediainfo = (subprocess.call(mediainfo_command))

所以我想要做的是将subprocess.call的输出保存到变量mediainfo中。但我不想在终端显示命令的实际输出。

这可能吗?

2 个答案:

答案 0 :(得分:1)

请改用subprocess.Popen.communicate。

print ("Getting MediaInfo...")
mediainfo_command = ["mediainfo", input_file_full]
proc = subprocess.Popen(mediainfo_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
mediainfo, mediaerr = proc.communicate()

答案 1 :(得分:0)

您可能还想查看subprocess.check_output

print ("Getting MediaInfo...")
mediainfo_command = ["mediainfo", input_file_full]
mediainfo = subprocess.check_output(mediainfo_command)