python执行shell实时输出,并返回code

2020-12-13 13:41

阅读:434

标签:lin   nes   readline   import   pytho   open   cmd   baidu   ufs   

import subprocess


def run_shell(shell):
    cmd = subprocess.Popen(shell, stdin=subprocess.PIPE, stderr=subprocess.PIPE,
                           stdout=subprocess.PIPE, universal_newlines=True, shell=True, bufsize=1)
    # 实时输出
    while True:
        line = cmd.stdout.readline()
        print(line, end='')
        if subprocess.Popen.poll(cmd) == 0:  # 判断子进程是否结束
            break

    return cmd.returncode


if __name__ == '__main__':
    print(run_shell("ping www.baidu.com"))

python执行shell实时输出,并返回code

标签:lin   nes   readline   import   pytho   open   cmd   baidu   ufs   

原文地址:https://www.cnblogs.com/chenqionghe/p/11532245.html


评论


亲,登录后才可以留言!