我正在使用 os.Popen 打开一个新的命令提示符窗口并运行一个进程。如何阅读该命令提示符中的文本。请帮助。
import os
def OpenServers():
os.chdir(coreServerFullPath)
process=os.popen("start cmd /K CoreServer.exe -c -s").read()
print(process) #Prints nothing
这是我要打印的命令提示符中显示的输出文本。
编辑:
我也试过这种方法,但是没有运气
from subprocess import Popen, PIPE, STDOUT
def OpenServers():
os.chdir(coreServerFullPath)
result = subprocess.Popen(['start', 'cmd', '/k', 'CoreServer.exe -c -s'], shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True)
time.sleep(4)
output=result.stdout.read()
print(output) #Prints nothing
新编辑:
我试过这样的事情。问题是,它让我跑了 2 次。我第一次运行时,控制台是空白的。第二次运行时,它运行正常,但出现错误,因为我只能打开一个服务器实例。
def Test1():
os.chdir(coreServerFullPath)
result = subprocess.check_output(['CoreServer.exe', '-c', '-s'])
print(result.stdout)
这是我尝试的完整代码。我只能以管理员身份运行 CoreServer,所以这样做
import os
import sys
import subprocess
from subprocess import Popen, CREATE_NEW_CONSOLE
from subprocess import Popen, PIPE, STDOUT
import time
import ctypes, sys
#The command prompts must be opened as administrators. 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)
#os.system("start /wait cmd /c {command}")
command_line = [coreServerFullPath, '-c', '-s']
result = subprocess.Popen(['start', 'cmd', '/k', 'CoreServer.exe -c -s'], shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True)
time.sleep(4)
output=result.stdout.read()
print(output)
#process=os.popen("start cmd /K CoreServer.exe -c -s").read()
#print(process)
def Test():
os.chdir(coreServerFullPath)
output = subprocess.check_output(['CoreServer.exe', '-c', '-s'],shell=True)
time.sleep(4)
print(output)
def Test1():
os.chdir(coreServerFullPath)
result = subprocess.check_output(['CoreServer.exe', '-c', '-s'])
print(result.stdout)
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):
Test1()
else:
# Re-run the program with admin rights
ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, " ".join(sys.argv), None, 1)