python执行shell实时输出,并返回code
2020-12-13 13:41
标签:lin nes readline import pytho open cmd baidu ufs python执行shell实时输出,并返回code 标签:lin nes readline import pytho open cmd baidu ufs 原文地址:https://www.cnblogs.com/chenqionghe/p/11532245.htmlimport 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"))
下一篇:C#中Listbox的用法