我试图获得实时输出以及完整输出。我有下面的代码,可以很好地输出命令的实时输出,但我还需要捕获完整的输出,然后可以在电子邮件中发送而不会中断实时输出
backup = subprocess.Popen("rsync ....", shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
for line in iter(backup.stdout.readline, b''):
print(">>> " + line.rstrip())
我尝试添加以下内容但会导致实时输出不显示
output = backup.stdout.read()
答案 0 :(得分:2)
为什么不沿途收集完整/完整输出?
backup = subprocess.Popen("rsync ....", shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
full = []
for line in iter(backup.stdout.readline, b''):
line = line.rstrip().decode('utf8')
print(">>>", line)
full.append(line)
output = '\n'.join(full)
print("full output:", output)