Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect subprocess to a variable as a string [duplicate]

Tags:

python

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.

like image 990
David542 Avatar asked Sep 09 '11 21:09

David542


1 Answers

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)
like image 53
Scott A Avatar answered Sep 17 '22 22:09

Scott A