简体   繁体   中英

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

the return output can not get the dynamic result. eg: if i execute the command "git clone xxx" by using this method, it cannot show all the information the "git clone xxx" shows on the stdout.

Try this:

import subprocess


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

#Output:
#b'hello\n'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM