Redirect subprocess to a variable as a string
Question: > **Possible Duplicate:**  
>  [Parsing a stdout in
> Python](http://stackoverflow.com/questions/2101426/parsing-a-stdout-in-
> python)

With the following command, it prints '640x360'

    
    
    >>> command = subprocess.call(['mediainfo', '--Inform=Video;%Width%x%Height%', 
    '/Users/david/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.

**Update** : answers can be found here: [Parsing a stdout in
Python](http://stackoverflow.com/questions/2101426/parsing-a-stdout-in-
python). This worked for me:

    
    
    >>> p1 = subprocess.Popen(['mediainfo', '--Inform=Video;%Width%x%Height%', 
        '/Users/david/Desktop/10stest720p.mov'],stdout=PIPE)
    >>> output=p1.communicate()[0].strip('\n')
    >>> output
    '1280x688'
    


Answer: 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)
    

