473,395 Members | 1,763 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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.

This works...
Expand|Select|Wrap|Line Numbers
  1. import subprocess, time, os, sys
  2.  
  3. cmd = "rsync.exe -vaz souce/ dest/"
  4.  
  5. p = subprocess.Popen(cmd,
  6.                      shell=True,
  7.                      bufsize=64,
  8.                      stdin=subprocess.PIPE,
  9.                      stderr=subprocess.PIPE,
  10.                      stdout=subprocess.PIPE)
  11.  
  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
2 7405
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
  1.  
  2. seek = 0
  3. line = ""
  4. extra_run = 5000 ## rsync.exe send returncode before terminated. Extra bytes.
  5. while True:
  6.     self.std.seek(seek)
  7.     byte = str(self.std.read(1),"windows-1252")
  8.     if byte == "\n" or byte == "\r":
  9.         print(line)
  10.         line = ""
  11.     else:
  12.         line += byte
  13.     seek += 1
  14.  
works like a charm... variables may differ from the above example.
Oct 20 '09 #2
bvdet
2,851 Expert Mod 2GB
newlasdjfk,

Thanks for posting back with your solution.

BV
Moderator
Oct 21 '09 #3

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

Similar topics

0
by: Roman Neuhauser | last post by:
Hello, I have a piece of code that gets run in a script that has its stdout closed: import sys sys.stdout = sys.stderr c = subprocess.Popen (..., stdin = subprocess.PIPE,
1
by: Jacek Popławski | last post by:
Popen from subprocess module gives me access to stdout, so I can read it. Problem is, that I don't know how much data is available... How can I read it without blocking my program? example:...
0
by: Christoph Haas | last post by:
Evening, I'm having trouble with running a process through Python 2.4's subprocess module. Example code: ======================================================== def run(command): run =...
8
by: cypher543 | last post by:
This has been driving me insane for the last hour or so. I have search everywhere, and nothing works. I am trying to use the subprocess module to run a program and get its output line by line. But,...
2
by: gbastian | last post by:
Dear Users, I am trying to recover through the python function popen3 the stdout,in,err of a launched process. I would like also to recover the stdout which you can get only through the...
2
by: Greg Ercolano | last post by:
When I use os.popen(cmd,'w'), I find that under windows, the stdout of the child process disappears, instead of appearing in the DOS window the script is invoked from. eg: C:\type foo.py import...
6
by: gregpinero | last post by:
Let's say I have this Python file called loop.py: import sys print 'hi' sys.stdout.flush() while 1: pass And I want to call it from another Python process and read the value 'hi'. How...
2
by: sophie_newbie | last post by:
Hi, I'm wondering if its possible to copy all of stdout's output to a string, while still being able to print on screen. I know you can capture stdout, but I still need the output to appear on the...
3
by: Thomas Jansson | last post by:
Dear all I have tkinkter based frontend to a Fortran based program. I use subprocess to launch the fortran program as a child process and I wish to see the output of the fortran program as it is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.