[英]Using variables as arguments for a command called by subprocess in Python 2.7.x
[英]passing special characters as arguments to subprocess in python (2.7.x)
我正在尝试在Linux中将mediainfo命令的输出存储在一个变量中。 我为此使用子流程模块。 问题是mediainfo命令的参数具有特殊字符。 这是片段
shell命令是:
mediainfo --Inform="Video;%DisplayAspectRatio%" test.mp4
和python代码是:
mediain = str('--Inform="Video;%DisplayAspectRatio%"')
mediaout = subprocess.check_output("medainfo", mediain ,"test.mp4")
print mediaout
错误即时通讯是
--Inform="Video;%DisplayAspectRatio%"
Traceback (most recent call last):
File "./test.py", line 8, in <module>
mediaout = subprocess.check_output("medainfo", '--Inform="Video;%DisplayAspectRatio%"',"test.mp4")
File "/usr/lib64/python2.7/subprocess.py", line 568, in check_output
process = Popen(stdout=PIPE, *popenargs, **kwargs)
File "/usr/lib64/python2.7/subprocess.py", line 660, in __init__
raise TypeError("bufsize must be an integer")
TypeError: bufsize must be an integer
在这方面的任何帮助都是python的绝对新手,谢谢
subprocess.check_output()
期望第一个参数为列表。 尝试这个:
args = ['mediainfo', '--Inform=Video;%DisplayAspectRatio%', 'test.mp4']
mediaout = subprocess.check_output(args)
print mediaout
最好将命令格式保留为字符串,并将其作为列表传递给subprocess.check_out()
:
cmd = 'mediainfo --Inform=Video;%DisplayAspectRatio% test.mp4'
mediaout = subprocess.check_out(cmd.split()) #will split cmd over whitespaces and pass to check_out as a list.
print mediaout
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.