Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
2 of 4
code formatting
nosklo
  • 224.2k
  • 58
  • 300
  • 299

catching stdout in realtime from subprocess

I have read tons of posts but still can't seem to figure it out.

I want to subprocess.Popen() rsync.exe in windows, and print the stdout in python.

My code works, but it doesn't catch the progress until a file is done transfered! I want to print the progress for each file in realtime.

Using python 3.1 now since I heard it should be better at handling IO.

import subprocess, time, os, sys cmd = "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() 
John A
  • 1.2k
  • 2
  • 9
  • 4