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
regards
STeve
--
Consulting, training, speaking: http://www.holdenweb.com/
Python Web Programming: http://pydish.holdenweb.com/pwp/
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/
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
# 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