开发者

Redirect subprocess to a variable as a string [duplicate]

This question already has answers here: Closed 11 years ago.

With the following command, it prints '640x360'

>>> command = subprocess.call(['mediainfo', '--Inform=Video;%Width%x%Height%', 
'/Users/Desktop/1video.mp4'])

640x360

How would I set a variable equal to the开发者_运维百科 string of the output, so I can get x='640x360'? Thank you.


If you're using 2.7, you can use subprocess.check_output():

>>> import subprocess
>>> output = subprocess.check_output(['echo', '640x360'])
>>> print output
640x360

If not:

>>> p = subprocess.Popen(['echo', '640x360'], stdout=subprocess.PIPE)
>>> p.communicate()
('640x360\n', None)


import subprocess
p = subprocess.Popen(["ls", "-al"], stdout=subprocess.PIPE)
out, err = p.communicate()
print out
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜