简体   繁体   中英

passing special characters as arguments to subprocess in python (2.7.x)

I am trying to store the out put of mediainfo command in linux in a variable. I am using the subprocess module for this. The problem is that the arguments for mediainfo command have special characters. here is the snippet

the shell command is:

 mediainfo --Inform="Video;%DisplayAspectRatio%" test.mp4

and the python code is:

mediain = str('--Inform="Video;%DisplayAspectRatio%"')
mediaout = subprocess.check_output("medainfo", mediain ,"test.mp4")
print mediaout

error im getting is

--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

any help in this regardd is aprreciated, absolute newbie in python Thanks

subprocess.check_output() expects the first argument to be a list. Try this:

args = ['mediainfo', '--Inform=Video;%DisplayAspectRatio%', 'test.mp4']
mediaout = subprocess.check_output(args)
print mediaout

Possibly better to keep the command format in a string and pass it to subprocess.check_out() as list:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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