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 |1 Answer
Reset to default 0Modify 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
版权声明:本文标题:cmd - Unable to read output from interactive script in python subprocess.Popen - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744875437a2629886.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
python test.py
, but notpython
– vIIr Commented Mar 9 at 8:59