# 获得运行结果,一次读取所有,要等待命令运行结束 p = subprocess.Popen('ping baidu.com', shell=True, stdout=subprocess.PIPE).stdout print(p.read().decode('gbk'))
# 获得运行结果,一次读取一行 p = subprocess.Popen('ping baidu.com', shell=True, stdout=subprocess.PIPE) for line initer(p.stdout.readline, b''): print(line.strip().decode('gbk'))