user3604938
user3604938

Reputation: 61

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

Upvotes: 6

Views: 2938

Answers (2)

Shan Valleru
Shan Valleru

Reputation: 3121

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

Upvotes: 1

Adam Bartoš
Adam Bartoš

Reputation: 717

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.

Upvotes: 1

Related Questions