468,140 Members | 1,433 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,140 developers. It's quick & easy.

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 1276

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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

54 posts views Thread by Brandon J. Van Every | last post: by
699 posts views Thread by mike420 | last post: by
226 posts views Thread by Stephen C. Waterbury | last post: by
14 posts views Thread by David MacQuigg | last post: by
2 posts views Thread by ajikoe | last post: by
26 posts views Thread by Christoph Zwerschke | last post: by
83 posts views Thread by Licheng Fang | last post: by
1 post views Thread by gcdp | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.