gpt4 book ai didi

python - 调用另一个 python 脚本的 main() 并获取 STDOUT 结果

转载 作者:太空宇宙 更新时间:2023-11-04 10:41:42 25 4
gpt4 key购买 nike

我正在尝试使用子进程调用另一个本地脚本。

本地脚本将结果打印到屏幕上。

本地:

def main():
"""
main function
controls the method call, the method is decided by TS runner
"""

sParams = ScriptParamHandler()
paramsList = ['TestName'] # ordered list of parameters
paramsDict = sParams.AnalyzeParametersForScript(paramsList)

mainReturn = None

with ProductXXXXX() as testSequence:

testSequence.testFixture.InitDevices()

func = getattr(testSequence, paramsDict['TestName'])

returnVal = func()

print paramsDict['TestName'], "\n", returnVal, "\n"

if __name__ == "__main__":
main()

调用脚本:

with BasicRunner(testSet, testSetLocation, paramsDict['ProfileName']) as testRunner:

if testRunner.CheckFolderAndFile():

testRunner.GetProfile()

for test in testRunner.testList:

testRunner.logger.Info("Test {} started...".format(test))

testResult = subprocess.call(testRunner.tsPyFile + " " + test, shell=True)

testRunner.logger.Info("Test {} result {}".format(test, testResult))

else:

pass

我希望调用者脚本 testResult 成为本地脚本的 stout

我试过 stdout=subprocess.PIPEsubprocess.check_output() 但没有成功,也许有人可以给我一些更好的方向?

最佳答案

通过@shshank评论解决,这是解决方案:

proc = subprocess.Popen(['python', testRunner.tsPyFile, test], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
stdoutValue = proc.communicate()[0]

关于python - 调用另一个 python 脚本的 main() 并获取 STDOUT 结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20266012/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com