admin管理员组

文章数量:1404927

The following program is used to simulate cmd.exe using subprocess.

import subprocess
import sys
import threading

def handle_output(shell):
    while shell:
        output = shell.stdout.readline(1)
        print(output, end="")
        sys.stdout.flush()

subprocess_shell = subprocess.Popen("cmd.exe", shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)

threading.Thread(target=handle_output, args=(subprocess_shell,), daemon=True).start()

while True:
    subprocess_shell.stdin.flush()
    cmd = input()
    subprocess_shell.stdin.write(cmd + "\n")

When I execute commands like dir, echo, etc. it works fine, but once I run an interactive program like python, there is no output.

Live output from Python subprocess that calls Python script
This is useful when executing python test.py, by setting os.environ['PYTHONUNBUFFERED'] = '1' and env=os.environ, but still, I got nothing on the output when executing python using that method.

It seems that this is related unbuffered-output but setting os.environ['PYTHONUNBUFFERED'] = '1' and env=os.environ isn't suitable for all situations.

When I type python, the program should have the following output

Python 3.10.6 (tags/v3.10.6:9c7b4bd, Aug  1 2022, 21:53:49) [MSC v.1932 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>

however, I got nothing on the output.

The following program is used to simulate cmd.exe using subprocess.

import subprocess
import sys
import threading

def handle_output(shell):
    while shell:
        output = shell.stdout.readline(1)
        print(output, end="")
        sys.stdout.flush()

subprocess_shell = subprocess.Popen("cmd.exe", shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)

threading.Thread(target=handle_output, args=(subprocess_shell,), daemon=True).start()

while True:
    subprocess_shell.stdin.flush()
    cmd = input()
    subprocess_shell.stdin.write(cmd + "\n")

When I execute commands like dir, echo, etc. it works fine, but once I run an interactive program like python, there is no output.

Live output from Python subprocess that calls Python script
This is useful when executing python test.py, by setting os.environ['PYTHONUNBUFFERED'] = '1' and env=os.environ, but still, I got nothing on the output when executing python using that method.

It seems that this is related unbuffered-output but setting os.environ['PYTHONUNBUFFERED'] = '1' and env=os.environ isn't suitable for all situations.

When I type python, the program should have the following output

Python 3.10.6 (tags/v3.10.6:9c7b4bd, Aug  1 2022, 21:53:49) [MSC v.1932 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>

however, I got nothing on the output.

Share Improve this question asked Mar 9 at 8:09 vIIrvIIr 92 bronze badges 1
  • ``` import functools print = functools.partial(print, flush=True) ``` This also works on python test.py, but not python – vIIr Commented Mar 9 at 8:59
Add a comment  | 

1 Answer 1

Reset to default 0

Modify the output handling to read bytes:


subprocess_shell = subprocess.Popen("cmd.exe", shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)  # removed text=True

def handle_output(shell):
    while shell:
        output = shell.stdout.read(1)  # Change from readline(1) to read(1). Reading byte-by-byte prevents buffering issues
        sys.stdout.buffer.write(output)  # Change print to write. Direct buffer writing maintains proper data flow
        sys.stdout.flush()

The issue happens because the interactive program uses text-mode and line-based reading, which breaks the interactive I/O handling.

本文标签: cmdUnable to read output from interactive script in python subprocessPopenStack Overflow