2017-07-24 112 views
0

我正在尝试获取实时输出以及完整输出。我有下面的代码输出指令的实时输出很好,但我还需要获取完整的输出,然后可以在电子邮件的形式发送,而无需中断实时输出使用子进程获取实时输出和全输出

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() 

回答

1

为什么不沿途收集完整/完整输出?

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)