【问题标题】:Running a CMD command with Python, printing the output AND getting the output as variable使用 Python 运行 CMD 命令,打印输出并将输出作为变量获取
【发布时间】:2020-01-07 13:51:01
【问题描述】:

我正在尝试构建一个脚本来使用 pyinstaller 自动构建桌面应用程序。我需要实现的一件事是运行 pyinstaller 命令并将它的输出作为字符串获取以进行进一步处理,同时将任何命令打印到 Powershell 窗口中。现在我只是在使用 os.system() 但这不会将命令的输出作为字符串返回,只有退出代码。

我已经尝试使用 subproces.run(),正如在许多其他问题中所展示的那样,但这并没有向我返回有用的输出,我将命令运行为

result = subprocess.run("pyinstaller app.py", shell=True, stdout=subprocess.PIPE)

正在尝试

result.stdout.decode('utf-8')

获取命令的输出,但没有返回给我。

【问题讨论】:

    标签: python windows powershell command


    【解决方案1】:

    subprocess.getoutput 可以在 linux 上运行...希望它也可以在 windows 上运行

    import subprocess
    
    result = subprocess.getoutput('pyinstaller app.py')
    print(result)
    

    【讨论】:

    • 遗憾的是它不会将命令输出打印到控制台。不符合我的要求。
    • 哦,对不起。更新了示例。现在打印了吗? :)
    猜你喜欢
    • 1970-01-01
    • 2015-06-19
    • 2015-08-20
    • 2018-07-24
    • 1970-01-01
    • 2017-09-21
    • 1970-01-01
    • 2020-05-13
    • 1970-01-01
    相关资源
    最近更新 更多