I referred to this Read Command Prompt output here. But I can't get it to work.
What I am trying to do is, I open a new command prompt window using subprocess.Popen and I want to run an exe file with some arguments. After I run that process, I want to capture the output or read the text in that command prompt.
When I say cmd = subprocess.Popen('cmd.exe /K "CoreServer.exe -c -s"',creationflags=CREATE_NEW_CONSOLE,shell=True,stdout=subprocess.PIPE)
it won't run the process at all.
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())
Please see this screenshot, I want to read the text in the command prompt.
Just in case, here is the full code.
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)
Perhaps this might be what you are looking for: Running shell command and capturing the output
The answer there is very in depth, and I would recommend reading in full. To summarize:
If you are using Python 3.5+, then likely you want to use the run
method in subprocess
import subprocess
result = subprocess.run(['CoreServer.exe', '-c', '-s'], stdout=subprocess.PIPE)
print(result.stdout)
If you are using Python 3-3.4, then you will want to use the check_output
method in subprocess
import subprocess
output = subprocess.check_output(['CoreServer.exe', '-c', '-s'])
print(output)