善用搜索

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

        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()
            if print_flag:
                print(line)
            if subprocess.Popen.poll(cmd) == 0:  # 判断子进程是否结束
                break
        code = cmd.returncode
        return code
发表评论
退出移动版