473,574 Members | 2,242 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1487

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("prog 1 > 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.Pope n(['prog1'], stdout=file1)

file2 = open('file2', 'w')
prog2 = subprocess.Pope n(['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.Pope n(['prog1'], stdout=file1)


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

prog1 = subprocess.Pope n(['prog1'], stdout=file1, stderr=subproce ss.STDOUT)

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

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

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

prog1 = subprocess.Pope n(['prog1'], stdout=file1, stderr=subproce ss.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.Pope n(['prog1'], stdout=file1,
stderr=subproce ss.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.Pope n(['prog1'], stdout=file1,
stderr=subpr ocess.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
6531
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 FRICKIN' COOL!!! ***MAN*** that would save me a buttload of work and make my life sooooo much easier!" As opposed to minor differences of this feature...
699
33577
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 capabilities, unfortunately. I'd like to know if it may be possible to add a powerful macro system to Python, while keeping its amazing syntax, and if it...
226
12403
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 "help", "copyright", "credits" or "license" for more information. >>> d1 = {'a':1} >>> d2 = {'b':2} >>> d3 = {'c':3}
49
2821
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 Vainio http://www.students.tut.fi/~vainio24
14
2839
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" in the subject of any post to this newsgroup. Please ignore the old thread. I've also updated my webpage http://ece.arizona.edu/~edatools/Python...
2
4437
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 */ #include <time.h>
26
3210
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 just came across a funny example in which the opposite is the case. The following is a binary search algorithm in Java. It searches a value in an...
83
3591
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 <algorithm> using namespace std;
0
7799
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8042
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8226
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7806
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8094
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6442
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5297
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
2235
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 we have to send another system
0
1053
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.