python equivalent of the following program

A

AndyL

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.
 
B

blue99

AndyL said:
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
 
S

Steven Bethard

AndyL said:
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
 
E

Edward Elliott

Steven said:
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)
 
A

AndyL

Edward said:
Steven said:
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.
 
A

AndyL

Edward said:
AndyL said:
Edward said:
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.
 
E

Edward Elliott

AndyL said:
Edward said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top