Redirect subprocess to a variable as a string [duplicate]
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
精彩评论