英文:
Read text from os.Popen that opens new command prompt
问题
import os
import subprocess
import time
import ctypes, sys
需要以管理员权限运行此脚本以打开命令提示符窗口
def is_admin():
try:
return ctypes.windll.shell32.IsUserAnAdmin()
except:
return False
if is_admin():
# 仅当具有管理员特权时才能运行程序。
# 获取脚本所在目录。
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)
# 这是MSBuild.exe的路径,以后我们可以获取MSBuild.exe作为独立程序并更改路径。
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=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, close_fds=True)
time.sleep(4)
output = result.stdout.read()
print(output)
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:
# 以管理员权限重新运行程序
ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, " ".join(sys.argv), None, 1)
英文:
I am using os.Popen to open a new command prompt window and run a process. How can I read the text within that command prompt?
import os
def OpenServers():
os.chdir(coreServerFullPath)
process=os.popen("start cmd /K CoreServer.exe -c -s").read()
print(process) #Prints nothing
This is the output text that's shown in the command prompt which I want to print.
Edit
I also tried this way, but no luck
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
Edit 2
I tried something like this. Problem is, it makes me run 2 times. The first time when I run, the console is blank. The second time when I run it works but gives me an error because I can only open one instance of the server,.
def Test1():
os.chdir(coreServerFullPath)
result = subprocess.check_output(['CoreServer.exe', '-c', '-s'])
print(result.stdout)
Here is the full code that I was trying. I can run CoreServer only as an admin so doing it like this
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 elevated 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 privileges.
#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)
答案1
得分: 1
对于这种情况,获取输出的最佳方法是将其转储到日志文件中,然后从文件中读取输出。(最好在运行程序之前创建日志文件)
这里有一个简单的示例:
result = subprocess.Popen('start cmd /k CoreServer.exe -c -s >> log.log 2>&1', shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True)
time.sleep(4)
with open('log.log', 'r') as logg:
print(logg.read())
这应该打印出所需的输出。希望对你有帮助。
英文:
For this type of situations the best way to get the output is to dump it to a log file and then read the output from the file. (Is better to create the log file before running the program)
Here you have a simple example:
result = subprocess.Popen('start cmd /k CoreServer.exe -c -s >> log.log 2>&1', shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True)
time.sleep(4)
with open('log.log', 'r') as logg:
print(logg.read())
This should print the desired output. Hope it works for you.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论