在Python子过程中执行脚本并获取输出

发布于 2025-02-08 20:17:13 字数 793 浏览 0 评论 0原文

我正在尝试通过一些参数执行一个脚本,并将其伸向缓冲区以进行进一步处理。现在的原始代码片段现在按以下 -

cmd = "/pathToScript/myscript.sh --endpoint "+ ip_addr + " --register '"+ reg_token + "' --output-file " + pathToOutputFile
os.system(cmd)

上述CMD运行并创建输出文件,并在其中编写MyHelper脚本生成的数据。

现在,我想运行相同的脚本,但是我希望输出为缓冲区作为返回值,而不是写入文件。

我将代码线重写如下,但不会将数据生成缓冲区。

            creds = subprocess.run(
                ["/pathToScript/myscript.sh", "--endpoint {0}".format(endpoint_ipaddr), "--register {0}".format(reg_token)],
                check=True,
                stdout=subprocess.PIPE,
                universal_newlines=True
            ).stdout
            print("creds")

我尝试使用subprocess.run()和call()命令,但两者都没有返回任何内容。关于执行脚本并将输出收集到返回的变量,我的修改后代码是否正确?

感谢您的帮助。

谢谢

I am trying to get execute one script with some arguments and getting its out to a buffer for further processing. The original code snippet which works now as below-

cmd = "/pathToScript/myscript.sh --endpoint "+ ip_addr + " --register '"+ reg_token + "' --output-file " + pathToOutputFile
os.system(cmd)

The above cmd runs and creates the output file where it write the data generated by myhelper script.

Now I want to run the same script, but instead of writing to file, I am expecting the output to a buffer as returned value.

I rewrite the code line as below, but it does not generates the data into buffer.

            creds = subprocess.run(
                ["/pathToScript/myscript.sh", "--endpoint {0}".format(endpoint_ipaddr), "--register {0}".format(reg_token)],
                check=True,
                stdout=subprocess.PIPE,
                universal_newlines=True
            ).stdout
            print("creds")

I tried with both subprocess.run() and call() command, but both did not return anything. Is my modified code is correct with respect to executing the script and getting the output collected to returned variable?

Appreciate your help on this.

thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文