需要进行哪些特定的语法更改或配置更改,以便将从Windows 10中的cmd.exe调用下面的Python3.7.7脚本的整个输出输出到同一个CMD.exe窗口中?
下面的问题是,最低级别脚本( someCommand.py )将触发它自己的控制台窗口,并与其输出一起启动,然后在它运行后立即关闭,这样来自最低级别脚本( someCommand.py )的输出不会返回到调用它的cmd.exe控制台窗口。
高级调用命令
下面是在cmd.exe窗口中运行的调用高级脚本的命令:
python topLevelScript.py "firstInputsPath" "secondInputsPath"
高级命令调用的脚本
以下是topLevelScript.py的内容
print("Inside topLevelScript.py script.")
import sys
import sharedFunctions as sharedfunc
pathToInputs1 = str(sys.argv[1])
pathToInputs2 = str(sys.argv[2])
pathToCalls = "C:\\some\\path\\"
commandToCalls = "someCommand.py"
print ('pathToInputs1:', pathToInputs1 )
print ('pathToInputs2:', pathToInputs2 )
sharedfunc.applyFoundation(commandToCalls, pathToCalls, pathToInputs1, pathToInputs2)
--称为二级的共享功能模块
sharedFunctions.py位于与topLevelScript.py相同的目录中,该目录也是由cmd.exe调用topLevelScript.py的相同目录。sharedFunctions.py的内容如下:
import subprocess
def applyFoundation(scriptName, workingDir, inputs1Path, inputs2Path ):
print("Inside sharedFunctions.py script and applyFoundation(..., ...) function. ")
print ('inputs1Path:', inputs1Path )
print ('inputs2Path:', inputs2Path )
print("scriptName is: " +scriptName)
print("workingDir is: " +workingDir)
proc = subprocess.Popen( scriptName,cwd=workingDir,stdout=subprocess.PIPE, shell=True)
while True:
line = proc.stdout.readline()
if line:
thetext=line.decode('utf-8').rstrip('\r|\n')
decodedline=ansi_escape.sub('', thetext)
print(decodedline)
else:
break
最低级别脚本,它错误地输出到一个新的cmd.exe窗口:
上述所有输出都在调用它的同一个cmd.exe窗口中打印其控制台输出,但上面的subprocess.Popen(...)命令的输出除外,该命令启动了以下someCommand.py,并将输出输出打印在一个新的子cmd.exe窗口中,该窗口启动后迅速销毁,而不记录任何输出:
print("Inside someCommand.py script. ")
import os
import subprocess
subprocess.run("some cli command", shell=True, check=True)请注意,除了由someCommand.py调用之外,subprocess.Popen(...)还位于与两个调用脚本不同的路径/目录中。
需要进行哪些特定的更改才能将来自
someCommand.py的输出打印在调用高级父程序topLevelScript.py和程序的cmd.exe控制台窗口中?E 252
@Gerrat的建议
下面是@Gerrat的建议,我将subprocess.Popen(...)行改为如下:
mycommand = workingDir+scriptName
cp = subprocess.run(mycommand, shell=True, check=True, capture_output=True, universal_newlines=True)
print(cp.stdout)但是,我仍然看到一个新的控制台窗口正在为最低级别的脚本创建和销毁的问题,而没有将它的任何输出传递给持久调用窗口。
发布于 2020-06-10 22:13:20
如果subprocess.run返回一个CompletedProcess,
我认为您可以按以下方式更改someCommand.py:
cp = subprocess.run("some cli command", shell=True, check=True, capture_output=True, universal_newlines=True)
print(cp.stdout)https://stackoverflow.com/questions/62313731
复制相似问题