473,511 Members | 13,105 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Iterating through 2 files simultaneously

Hi folks,

I am trying to tee off both stdout and stderr from a process run
through Popen.
As a test, I am first trying to print the output below:

from subprocess import Popen,PIPE
....
p1 = Popen(['cvs', 'update'], stdout=PIPE, stderr=PIPE)
for (l1, l2) in zip(p1.stdout, p1.stderr):
print '-->' + l1,
print '-->' + l2,

This doesn't work - probably because I cannot iterate through the
pipes this way.

I am new to Python, and I'd appreciate it if you could please explain
why this
doesn't work and/or suggest an alternate way to redirect stdout and
stderr to a
common place.

My objective is for my code to print out stdout/stderr messages and at
the same
time redirect them to a log file.

Thanks

Mahesh
Jul 27 '08 #1
2 1294
wrote in news:7ae96aff-c1a7-4763-8db7-
ff**********@u6g2000prc.googlegroups.com in comp.lang.python:
Hi folks,

I am trying to tee off both stdout and stderr from a process run
through Popen.
As a test, I am first trying to print the output below:

from subprocess import Popen,PIPE
...
p1 = Popen(['cvs', 'update'], stdout=PIPE, stderr=PIPE)
for (l1, l2) in zip(p1.stdout, p1.stderr):
print '-->' + l1,
print '-->' + l2,

This doesn't work - probably because I cannot iterate through the
pipes this way.

I am new to Python, and I'd appreciate it if you could please explain
why this
doesn't work and/or suggest an alternate way to redirect stdout and
stderr to a
common place.

My objective is for my code to print out stdout/stderr messages and at
the same
time redirect them to a log file.
From the manual <url: http://docs.python.org/lib/node528.html>:

stdin, stdout and stderr specify ...
.... Additionally, stderr can be STDOUT, which indicates that the stderr
data from the applications should be captured into the same file handle
as for stdout.

So import STDOUT and make stderr=STDOUT in the Popen call, you will then
have one file/pipe to deal with p1.stdout.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 27 '08 #2
>
So import STDOUT and make stderr=STDOUT in the Popen call, you will then
have one file/pipe to deal with p1.stdout.
Thank you - that works great!

Mahesh
Jul 27 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

44
3992
by: Xah Lee | last post by:
here's a large exercise that uses what we built before. suppose you have tens of thousands of files in various directories. Some of these files are identical, but you don't know which ones are...
0
1250
by: ischenko | last post by:
Hi, I'm trying to find all modules that contain doctests and execute them (using DocTestSuite). The problem is how to iterate (programmatically) through the package's modules. >>> import M...
7
9048
by: erniedude | last post by:
Hi, I'm a newbie and I was wondering if anyone knew a (Python) script to run 4 batch files, one after the other (assuming the directories are known). It would be better if all 4 batch files...
5
1910
by: David | last post by:
Hi all: I am processing a 3D bitmaps(essentially ~1024 2D bitmaps with a size of 1MB each). If I want read large amount of radom data from this series, how could I buffer the file to get...
6
3605
by: asn | last post by:
Example : If i store details of Employee no and name in a text file and the employee salaries in another. Is it possible to read data from these two files simultaneously using file stream...
3
2044
by: Dan Kjaergaard | last post by:
Hi I created a program using C#, which iterates files and displays these in a listview. However I would like to filter the files, so files won't be displayed if the current user hasn't "read...
3
1212
by: AVL | last post by:
Hi, I need some info on text and xml files...... How many users can simultaneously read a text or xml file... I've a web appliaction which needs to access a text file and a xml file.. The...
35
1655
by: Mokkapati | last post by:
Hi all, I have data that has to be written successively, that is, this data is updated at every step in the loop and I need to print this in a file. So, I need something like, for every step,...
7
14602
by: Tyrone Showers | last post by:
I have a problem of getting the error "too many files open" and would like to trace my application. However, I have found nothing about how to display the current number of open files. Does...
1
1563
by: bay_dar | last post by:
It seems there has got to be a better way to work with log files where I want to keep 8 days of logs. For instance if I wanted to keep 80 days, this would be a horrible approach. How can I make...
0
7144
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7356
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
7512
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...
0
5671
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4741
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3227
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1577
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
785
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
449
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.