在 python 的新窗口中读取命令提示符输出

问题描述 投票:0回答:1

希望它不是重复的。我在这里提到了这个Read Command Prompt output。但我无法让它工作。

我想做的是,我使用 subprocess.Popen 打开一个新的命令提示符窗口,我想运行一个带有一些参数的 exe 文件。运行该过程后,我想捕获输出或阅读该命令提示符中的文本。请帮助。

当我说

cmd = subprocess.Popen('cmd.exe /K "CoreServer.exe -c -s"',creationflags=CREATE_NEW_CONSOLE,shell=True,stdout=subprocess.PIPE)
它根本不会运行这个过程。

import subprocess
from subprocess import Popen, CREATE_NEW_CONSOLE

    def OpenServers():
            os.chdir(coreServerFullPath)
            cmd = subprocess.Popen('cmd.exe /K "CoreServer.exe -c -s"',creationflags=CREATE_NEW_CONSOLE)
            time.sleep(3)
            os.chdir(echoServerFullPath)
            #cmd.exe /K "EchoServer.exe -c -s"
            cmd1=subprocess.Popen('cmd.exe /K "EchoServer.exe -c -s"',creationflags=CREATE_NEW_CONSOLE)
            #subprocess.Popen(['runas', '/user:Administrator', '"CoreServer.exe -c -s"'],creationflags=CREATE_NEW_CONSOLE
            print("OUTPUT 1 "+cmd.stdout.readline())

请看这张截图,我想阅读命令提示符中的文字。

以防万一,这里是完整的代码。

import os
import subprocess
from subprocess import Popen, CREATE_NEW_CONSOLE
import time
import ctypes, sys

#The command prompts must be opened as administrator. So need to run the python script with elebvated permissions. Or else it won't work
def is_admin():
    try:
        return ctypes.windll.shell32.IsUserAnAdmin()
    except:
        return False

if is_admin():
    #The program can only run with elevated admin previlages.
    
    #Get the directory where the file is residing.
    currentDirectory=os.path.dirname(os.path.abspath(__file__))
    coreServerFullPath=os.path.join(currentDirectory,"Core\CoreServer\Server\CoreServer/bin\Debug")
    isExistCoreServer=os.path.exists(coreServerFullPath)

    echoServerFullPath=os.path.join(currentDirectory,"Echo\Server\EchoServer/bin\Debug")
    isExistEchoServer=os.path.exists(echoServerFullPath)

    #For now this is the MSBuild.exe path. Later we can get this MSBuild.exe as a standalone and change the path.
    msBuildPath="C:\Program Files (x86)\Microsoft Visual Studio/2019\Professional\MSBuild\Current\Bin/amd64"
    pathOfCorecsProjFile=os.path.join(currentDirectory,"Core\CoreServer\Server\CoreServer\CoreServer.csproj")
    pathOfEchocsProjFile=os.path.join(currentDirectory,"Echo\Server\EchoServer\EchoServer.csproj")


    def OpenServers():
        os.chdir(coreServerFullPath)
        cmd = subprocess.Popen('cmd.exe /K "CoreServer.exe -c -s"',creationflags=CREATE_NEW_CONSOLE)
        time.sleep(3)
        os.chdir(echoServerFullPath)
        #cmd.exe /K "EchoServer.exe -c -s"
        cmd1=subprocess.Popen('cmd.exe /K "EchoServer.exe -c -s"',creationflags=CREATE_NEW_CONSOLE)
        #subprocess.Popen(['runas', '/user:Administrator', '"CoreServer.exe -c -s"'],creationflags=CREATE_NEW_CONSOLE
       



    if(not isExistCoreServer):
        if(os.path.isfile(pathOfCorecsProjFile)):
            os.chdir(msBuildPath)
            startCommand="start cmd /c"
            command="MSBuild.exe "+pathOfCorecsProjFile+" /t:build /p:configuration=Debug"
            #os.system(startCommand+command)
            cmd=subprocess.Popen(startCommand+command)

    if(not isExistEchoServer):
        if(os.path.isfile(pathOfEchocsProjFile)):
            os.chdir(msBuildPath)
            startCommand="start cmd /c"
            command="MSBuild.exe "+pathOfEchocsProjFile+" /t:build /p:configuration=Debug"
            os.system(startCommand+command)

    if(isExistCoreServer and isExistEchoServer):
        OpenServers()

else:
    # Re-run the program with admin rights
    ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, " ".join(sys.argv), None, 1)
python subprocess
1个回答
0
投票

也许这可能是您正在寻找的:运行 shell 命令并捕获输出

那里的答案很深入,我会推荐完整阅读。总结:

Python 3.5+

如果您使用的是 Python 3.5+,那么您可能想在

run
 中使用 
subprocess

方法
import subprocess

result = subprocess.run(['CoreServer.exe', '-c', '-s'], stdout=subprocess.PIPE)
print(result.stdout)

蟒蛇 3-3.4

如果您使用的是 Python 3-3.4,那么您将需要使用

check_output
 中的 
subprocess

方法
import subprocess

output = subprocess.check_output(['CoreServer.exe', '-c', '-s'])
print(output)
© www.soinside.com 2019 - 2024. All rights reserved.