Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capturing console output in Python

I can capture the output of a command line execution, for example

import subprocess
cmd = ['ipconfig']
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE)
output = proc.communicate()[0]
print output

In the above example it is easy as ipconfig, when run directly on the command line, will print the output on the same console window. However, there can be situations where the command opens a new console window to display all the output. An example of this is running VLC.exe from the command line. The following VLC command will open a new console window to display messages:

vlc.exe -I rc XYZ.avi

I want to know how can I capture the output displayed on this second console window in Python. The above example for ipconfig does not work in this case.

Regards

SS

like image 660
user3604938 Avatar asked Jun 25 '14 10:06

user3604938


2 Answers

I think this is really matter of VLC interface. There may be some option to vlc.exe so it doesn't start the console in new window or provides the output otherwise. In general there is nothing that stops me to run a process which runs another process and does not provide its output to caller of my process.

like image 123
user87690 Avatar answered Oct 13 '22 07:10

user87690


Note that, in your second command, you have multiple arguments (-I, rc, and the file name - XYZ.avi), in such cases, your cmd variable should be a list of - command you want to run , followed by all the arguments for that command:

Try this:

import subprocess
cmd=['vlc.exe',  '-I', 'rc', 'XYZ.avi']
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE)
output = proc.communicate()[0]
print output

If you are not running the script from right directory, you might want to provide absolute paths for both vlc.exe and your XYZ.avi in cmd var

like image 1
Shan Valleru Avatar answered Oct 13 '22 06:10

Shan Valleru