By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,662 Members | 929 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,662 IT Pros & Developers. It's quick & easy.

catching stdout in realtime from subprocess

P: 3
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.

This works...
Expand|Select|Wrap|Line Numbers
  1. import subprocess, time, os, sys
  3. cmd = "rsync.exe -vaz souce/ dest/"
  5. p = subprocess.Popen(cmd,
  6.                      shell=True,
  7.                      bufsize=64,
  8.                      stdin=subprocess.PIPE,
  9.                      stderr=subprocess.PIPE,
  10.                      stdout=subprocess.PIPE)
  12. for line in p.stdout:
  13.     print(">>> " + str(line.rstrip()))
  14.     p.stdout.flush()
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 better to handle IO.
Oct 16 '09 #1
Share this Question
Share on Google+
2 Replies

P: 3
Well, I found out myslelf.
I case someone else needs it... here it is. Apparently pythons built in:
for line in file
don't consider a linebreak with \r as a true linebreak (at least not in windows). So since I couldn't find out how to get this stdout with universal linebreak, so I made my own loop:

Expand|Select|Wrap|Line Numbers
  2. seek = 0
  3. line = ""
  4. extra_run = 5000 ## rsync.exe send returncode before terminated. Extra bytes.
  5. while True:
  7.     byte = str(,"windows-1252")
  8.     if byte == "\n" or byte == "\r":
  9.         print(line)
  10.         line = ""
  11.     else:
  12.         line += byte
  13.     seek += 1
works like a charm... variables may differ from the above example.
Oct 20 '09 #2

Expert Mod 2.5K+
P: 2,851

Thanks for posting back with your solution.

Oct 21 '09 #3

Post your reply

Sign in to post your reply or Sign up for a free account.