Capturing console output in Python

I can capture command line execution output like

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

In the above example, this is easy, since ipconfig, when it is run directly on the command line, will print the output in the same console window. However, situations may arise when a command opens a new console window to display all 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 I can capture the output displayed in this second console window in Python. The above example for ipconfig does not work in this case.

Hello

SS

+6
source share
2 answers

I think this is really a VLC interface issue. There may be some option vlc.exe so that it does not start the console in a new window or does not produce a result otherwise. In general, nothing prevents me from starting a process that starts another process and does not provide its output to the calling process.

+1
source

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

Try the following:

 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 using a script from the right directory, you can specify absolute paths for both vlc.exe and your XYZ.avi in โ€‹โ€‹cmd var

0
source

Source: https://habr.com/ru/post/971333/


All Articles