如果我使用 python 到 运行 一个系统命令,并且想得到它的动态结果,我应该怎么做?

If I use python to run a system command, and want to get it's dynamic result, what should i do?

@staticmethod
    def execute_cmd(cmd):
        exitcode, output = subprocess.getstatusoutput(cmd)
        if exitcode != 0:
            MyLog.error(f'{cmd} executes failed!')
            raise Exception(f'{cmd} executes failed!')

        return output

return输出得不到动态结果。 例如:如果我使用此方法执行命令“git clone xxx”,它无法显示“git clone xxx”在标准输出上显示的所有信息。

试试这个:

import subprocess


ressult = subprocess.check_output("echo 'hello'", shell=True)
print(ressult)

#Output:
#b'hello\n'