Ask Your Question
2

How can one view CMD messages while executing an executable through CMD using Python?

asked 2021-10-18 11:00:00 +0000

ladyg gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2022-09-07 02:00:00 +0000

pufferfish gravatar image

One way to view CMD messages while executing an executable through CMD using Python is to use the subprocess module. Here's an example code snippet:

import subprocess

cmd = 'my_executable.exe arg1 arg2'
# Replace 'my_executable.exe' and 'arg1 arg2' with the actual executable and arguments

p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)

# Read the output line by line as it's generated
while True:
    line = p.stdout.readline()
    if not line:
        break
    print(line.strip().decode())

This code snippet creates a new process using the Popen function from the subprocess module, passing in the command and arguments as a string, and redirecting both stdout and stderr to a pipe. Then it reads the output line by line as it's generated using a loop, printing each line to the console. This way, you can see the CMD messages in real-time as they're generated by the executable.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-10-18 11:00:00 +0000

Seen: 7 times

Last updated: Sep 07 '22