简体   繁体   English

如何在子进程弹出的情况下清除“cmd.exe”的 STDOUT?

[英]How to clear the STDOUT of 'cmd.exe' with subprocess popen?

Problem问题

The code below is a simulation of a real terminal, in this case, a CMD terminal.下面的代码是一个真实终端的模拟,在本例中是一个 CMD 终端。 The problem is that the "cls" don't clear the STDOUT of CMD.问题是“cls”没有清除 CMD 的 STDOUT。 So, the string STDOUT start to stay so much extensive.因此,字符串 STDOUT 开始保持如此广泛的范围。

Example of problem问题示例

Microsoft Windows [versÆo 10.0.19042.746] (c) 2020 Microsoft Corporation. Microsoft Windows [版本 10.0.19042.746] (c) 2020 Microsoft Corporation。 Todos os direitos reservados. Todos os direitos reservados。

C:\Users\Lsy\PycharmProjects\Others>chdir C:\Users\Lsy\PycharmProjects\Others>chdir

C:\Users\Lsy\PycharmProjects\Others C:\Users\Lsy\PycharmProjects\Others

C:\Users\Lsy\PycharmProjects\Others>echo test C:\Users\Lsy\PycharmProjects\Others>回声测试

test测试

C:\Users\Lsy\PycharmProjects\Others>cls C:\Users\Lsy\PycharmProjects\Others>cls

Type:类型:

Question问题

How to clear the STDOUT?如何清除 STDOUT?

Script脚本

import subprocess

f = open('output.txt', 'w')
proc = subprocess.Popen('cmd.exe', stderr=subprocess.STDOUT, stdin=subprocess.PIPE, stdout=f, shell=True)

while True:
    command = input('Type:')
    command = command.encode('utf-8') + b'\n'

    proc.stdin.write(command)
    proc.stdin.flush()
    with open('output.txt', 'r') as ff:
        print(ff.read())
        ff.close()

This is not how I recommend using sub processes - but I'm assuming you have some reason for doing things this way...这不是我推荐使用子流程的方式 - 但我假设你有一些理由这样做......

Given:鉴于:

  1. You've directed the CMD sub process to STDOUT to a file called "output.txt".您已将 CMD 子进程定向到 STDOUT 到名为“output.txt”的文件。
  2. The CLS character is captured in the output.txt. CLS 字符在 output.txt 中捕获。
  3. Your terminal then displaying the contents of the "output.txt" file (which is not ever cleared) and leaves a mess.然后您的终端会显示“output.txt”文件的内容(从未清除过)并留下一团糟。

Therefore: If you want to "clear" your sub process terminal, then you will have to flush your "output.txt" file.因此:如果你想“清除”你的子进程终端,那么你将不得不刷新你的“output.txt”文件。 You can trivially do this by processing on the "command" variable before encoding and sending it to the sub process.您可以通过在编码之前处理“command”变量并将其发送到子进程来轻松做到这一点。
eg:例如:

import subprocess
import os
f = open('output.txt', 'w')
proc = subprocess.Popen('cmd.exe', stderr=subprocess.STDOUT, stdin=subprocess.PIPE, stdout=f, shell=True)
while True:
    command = input('Type:')
    if command == "cls":
        open('output.txt', 'w').close()
        os.system('cls' if os.name == 'nt' else 'clear')
    else:
        command = command.encode('utf-8') + b'\n'
        proc.stdin.write(command)
        proc.stdin.flush()
        with open('output.txt', 'r+') as ff:
            print(ff.read())

You could maybe also not redirect the output to a text file...您也可以不将 output 重定向到文本文件...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM