473,414 Members | 1,738 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,414 software developers and data experts.

python equivalent of the following program

Hi,

What would by a python equivalent of following shell program:

#!/bin/sh

prog1 > file1 &
prog2 > file2 &
As you see, I need to spawn a few processes and redirect stdout to some
files.

Thx,

A.
May 11 '06 #1
6 1479

AndyL wrote:
Hi,

What would by a python equivalent of following shell program:

#!/bin/sh

prog1 > file1 &
prog2 > file2 &
As you see, I need to spawn a few processes and redirect stdout to some
files.


For example :

------------------cut here-------------------------------
#!/usr/bin/env python

import os

# 1-st variant

os.system("prog1 > tmp1.txt &")

# or 2-nd variant

os.popen("prog1 > tmp2.txt &")
---------------cut here-------------------------------------

Regards,
Rob

May 11 '06 #2
AndyL wrote:
What would by a python equivalent of following shell program:

#!/bin/sh

prog1 > file1 &
prog2 > file2 &


If you're just going for quick-and-dirty, Rob's suggestion of os.system
is probably a reasonable way to go. If you want better error reporting,
I suggest using open() and the subprocess module:

import subprocess

file1 = open('file1', 'w')
prog1 = subprocess.Popen(['prog1'], stdout=file1)

file2 = open('file2', 'w')
prog2 = subprocess.Popen(['prog2'], stdout=file2)

If at some point later you want to make sure that the processes
completed, you simply call .wait() on prog1 or prog2.

STeVe
May 11 '06 #3
Steven Bethard wrote:
import subprocess

file1 = open('file1', 'w')
prog1 = subprocess.Popen(['prog1'], stdout=file1)


And if the script runs somewhere that stderr is likely to disappear:

prog1 = subprocess.Popen(['prog1'], stdout=file1, stderr=subprocess.STDOUT)

May 11 '06 #4
Edward Elliott wrote:
Steven Bethard wrote:
import subprocess

file1 = open('file1', 'w')
prog1 = subprocess.Popen(['prog1'], stdout=file1)

And if the script runs somewhere that stderr is likely to disappear:

prog1 = subprocess.Popen(['prog1'], stdout=file1, stderr=subprocess.STDOUT)


Forgot to mention before that the main motivation is to have the same
code on bot Linux and M$ platforms.
Does subprocess work well on both?

Also how to find out that the 'prog1' e.g. has exited and it is done?

Thx,
A.
May 12 '06 #5
AndyL wrote:
Edward Elliott wrote:
And if the script runs somewhere that stderr is likely to disappear:

prog1 = subprocess.Popen(['prog1'], stdout=file1,
stderr=subprocess.STDOUT)
Forgot to mention before that the main motivation is to have the same
code on bot Linux and M$ platforms.

Does subprocess work well on both?


yes

Also how to find out that the 'prog1' e.g. has exited and it is done?


prog1.wait() or prog1.poll(). look at the subprocess docs.
May 12 '06 #6
Edward Elliott wrote:
AndyL wrote:
Edward Elliott wrote:
And if the script runs somewhere that stderr is likely to disappear:

prog1 = subprocess.Popen(['prog1'], stdout=file1,
stderr=subprocess.STDOUT)


Forgot to mention before that the main motivation is to have the same
code on bot Linux and M$ platforms.

Does subprocess work well on both?

yes
Also how to find out that the 'prog1' e.g. has exited and it is done?

prog1.wait() or prog1.poll(). look at the subprocess docs.


thx a lot.
May 12 '06 #7

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

Similar topics

54
by: Brandon J. Van Every | last post by:
I'm realizing I didn't frame my question well. What's ***TOTALLY COMPELLING*** about Ruby over Python? What makes you jump up in your chair and scream "Wow! Ruby has *that*? That is SO...
699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
226
by: Stephen C. Waterbury | last post by:
This seems like it ought to work, according to the description of reduce(), but it doesn't. Is this a bug, or am I missing something? Python 2.3.2 (#1, Oct 20 2003, 01:04:35) on linux2 Type...
49
by: Ville Vainio | last post by:
I don't know if you have seen this before, but here goes: http://text.userlinux.com/white_paper.html There is a jab at Python, though, mentioning that Ruby is more "refined". -- Ville...
14
by: David MacQuigg | last post by:
I am starting a new thread so we can avoid some of the non-productive argument following my earlier post "What is good about Prothon". At Mr. Hahn's request, I will avoid using the name "Prothon"...
2
by: ajikoe | last post by:
Hi, I tried to follow the example in swig homepage. I found error which I don't understand. I use bcc32, I already include directory where my python.h exist in bcc32.cfg. /* File : example.c...
26
by: Christoph Zwerschke | last post by:
You will often hear that for reasons of fault minimization, you should use a programming language with strict typing: http://turing.une.edu.au/~comp284/Lectures/Lecture_18/lecture/node1.html I...
83
by: Licheng Fang | last post by:
Hi, I'm learning STL and I wrote some simple code to compare the efficiency of python and STL. //C++ #include <iostream> #include <string> #include <vector> #include <set> #include...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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
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
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...

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.