简体   繁体   English

Python subprocess.check_output

[英]Python subprocess.check_output

I am trying to use a couple shell level commands from Python to get and set variables for later use in my program but it does not appear to be calling the commands properly as the outputs should be simple 1-liners. 我正在尝试使用来自Python的几个shell级命令来获取和设置变量,以供以后在我的程序中使用,但由于输出应该是简单的1线性,因此它似乎并未正确调用命令。 Not quite sure if it's due to the % or ; 不太确定是否是由于%或; signs. 迹象。

current_vcodec = subprocess.check_output(["mediainfo", "--Inform='Video;%CodecID%'", "%s" % source])
current_acodec = subprocess.check_output(["mediainfo", "--Inform='Audio;%CodecID%'", "%s" % source])
duration = subprocess.check_output(["mediainfo", "--Inform='Video;%Duration%'", "%s" % source])

I highly recommend you use Kenneth Reitz's envoy python wrapper for subprocess. 我强烈建议您使用Kenneth Reitz的特使 python包装器进行子处理。 It makes calls to command line far easier to use. 它使对命令行的调用更加容易使用。

import envoy
res = envoy.run("mediainfo --Inform='Video;%CodecID%' {0}".format(source))
if res.status_code != 0:
   print("media info failure: {0}".format(res.std_out + " " + res.std_err))
 else:
   print(res.std_out)
   current_vcodec = res.std_out

If you needed, you can "escape" the % with a backslash, but I'm not sure that's the problem. 如果需要,可以用反斜杠“转义”%,但是我不确定这是问题所在。 The ; 的; shouldn't be a problem. 应该不是问题。

Ok, I got it working. 好吧,我知道了。 This is the syntax that gave the correct output. 这是给出正确输出的语法。

current_vcodec = subprocess.check_output("mediainfo --Inform='Video;%%CodecID%%' %s" % source, shell=True)
current_acodec = subprocess.check_output("mediainfo --Inform='Audio;%%CodecID%%' %s" % source, shell=True)
duration = subprocess.check_output("mediainfo --Inform='Video;%%Duration%%' %s" % source, shell=True)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM