我想subprocess.Popen()在Windows中使用rsync.exe,并在Python中打印标准输出。我的代码可以运行,但是直到文件传输完成后才能捕获进度!我想实时打印每个文件的进度。因为我听说现在应该使用Python 3.1,所以它在处理IO方面应该更好。import subprocess, time, os, syscmd = "rsync.exe -vaz -P source/ dest/"p, line = True, 'start'p = subprocess.Popen(cmd, shell=True, bufsize=64, stdin=subprocess.PIPE, stderr=subprocess.PIPE, stdout=subprocess.PIPE)for line in p.stdout: print(">>> " + str(line.rstrip())) p.stdout.flush()
3 回答

一只斗牛犬
TA贡献1784条经验 获得超2个赞
我知道这是一个老话题,但是现在有一个解决方案。使用--outbuf = L选项调用rsync。例:
cmd=['rsync', '-arzv','--backup','--outbuf=L','source/','dest']
p = subprocess.Popen(cmd,
stdout=subprocess.PIPE)
for line in iter(p.stdout.readline, b''):
print '>>> {}'.format(line.rstrip())
添加回答
举报
0/150
提交
取消