473,609 Members | 1,965 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 1488

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
6539
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 here, that feature there. Variations on style are of no interest to me. I'm coming at this from a...
699
33775
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 could be possible to add Pythonistic syntax to Lisp or Scheme, while keeping all of the...
226
12469
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
2832
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
2844
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 Anyone with some good ideas for "Python 3" is welcome to contribute. I hope GvR won't sue me for...
2
4439
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
3219
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 ordered array a of ints: public static int binarySearch(int a, int key) {
83
3608
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
8145
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8095
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8410
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7030
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6068
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5526
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4103
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2541
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
1407
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.