Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Redirecting stdin/stdout/setderr and os.execv

428 views
Skip to first unread message

Richard Philips

unread,
Jan 16, 2002, 10:25:20 AM1/16/02
to

How do I redirect stdin, stdout en stderr from within a python script
together with os.execv:


import os
import sys

sys.stdout = open("/tmp/output","wb")
os.execv("/bin/ls", ["ls", "-ls"])


does not redirect the stdout of the /bin/ls

Any ideas on this ?

I am looking for a replacement for os.system which is independent of the
pecularities of the shell in use.

Thank you,


Richard

Steve Holden

unread,
Jan 16, 2002, 11:59:58 AM1/16/02
to
"Richard Philips" <Richard...@ua.ac.be> wrote ...
You probably need the os.popen() function or one of its buddies
popen2.popen2(), os.popen3(), popen2.popen3() , etc, which create file-like
pipeline objects you can read the output from and write standard input to.
In short, look in modules os and popen2 for names beginning with popen!

regards
STeve
--
Consulting, training, speaking: http://www.holdenweb.com/
Python Web Programming: http://pydish.holdenweb.com/pwp/

Jason Orendorff

unread,
Jan 16, 2002, 11:14:19 AM1/16/02
to
> How do I redirect stdin, stdout en stderr from within a python script
> together with os.execv:

import os

# POSIX constants
STDIN_FILENO = 0
STDOUT_FILENO = 1
STDERR_FILENO = 2

out_fd = os.open("/tmp/output", os.O_WRONLY | os.O_CREAT)
os.dup2(out_fd, STDOUT_FILENO)


os.execv("/bin/ls", ["ls", "-ls"])

> I am looking for a replacement for os.system which is


> independent of the pecularities of the shell in use.

You may find that you want to fork() before doing
execv(), then, as execv() replaces your Python process
and does not return!

Using os.fork(), os.dup2(), and os.exec*(), plus os.pipe(),
you can write non-shell versions of os.popen() and the
popen2 functions.

You can also write
f = open("/tmp/output", "w")
out_fd = f.fileno()
to get the file descriptor.

Consider getting a book by W. Richard Stevens.

## Jason Orendorff http://www.jorendorff.com/

Richard Philips

unread,
Jan 16, 2002, 12:40:23 PM1/16/02
to
Jason Orendorff wrote:

I understand the solution, and it solves the problem on UNIX.

But os.fork() is not usable on win32.

I would like a solution which work both on UNIX and windows !


Thank you,


Richard Philips
University of Antwerp


Jason Orendorff

unread,
Jan 16, 2002, 2:06:08 PM1/16/02
to
Richard Philips wrote:
> I understand the solution, and it solves the problem on UNIX.
> But os.fork() is not usable on win32.
>
> I would like a solution which work both on UNIX and windows !

# Use spawnv() instead of fork()/execv().
# This works for me on Windows 2000.

import os, sys

STDIN_FILENO = 0
STDOUT_FILENO = 1
STDERR_FILENO = 2

def run(program, argv, outfile):
orig_stdout = os.dup(STDOUT_FILENO)
try:
new_stdout = os.open(outfile, os.O_WRONLY | os.O_CREAT | os.O_TRUNC)
try:
os.dup2(new_stdout, STDOUT_FILENO)
try:
return os.spawnv(os.P_WAIT, program, argv)
finally:
os.dup2(orig_stdout, STDOUT_FILENO)
finally:
os.close(new_stdout)
finally:
os.close(orig_stdout)


status = run("C:\\programs\\cygwin\\bin\\ls.exe",
["ls", "-1"],
"output.txt")
print "ls completed with status:", status

0 new messages