Redirect DOS command output 
Author Message
 Redirect DOS command output

Hey, I'm using os.system() to execute a DOS command (on an NT machine).
I would like to capture (and parse) the output from the command. I'm
familiar with sys.stout but I'm not sure how to do this.

Thanks for the help.

Sent via Deja.com http://www.*-*-*.com/
Before you buy.



Mon, 29 Jul 2002 03:00:00 GMT  
 Redirect DOS command output

Quote:

> Hey, I'm using os.system() to execute a DOS command (on an NT

machine).

After doing some more research, I came across win32pipe.popen() and that
did the trick.

*******
import win32pipe

output = win32pipe.popen("dir", "r")
print output.read()
*******

Just out of curiosity, is there a cross platform way to do the above?
Maybe using os.pipe ? thanks

Sent via Deja.com http://www.deja.com/
Before you buy.



Mon, 29 Jul 2002 03:00:00 GMT  
 Redirect DOS command output

Quote:

>Hey, I'm using os.system() to execute a DOS command (on an NT machine).
>I would like to capture (and parse) the output from the command. I'm
>familiar with sys.stout but I'm not sure how to do this.

You either need to redirect the output to a file (e.g.

   os.system("foo > bar")

) or capture the output directly:

   os.popen("foo")

Note that os.popen() is known to have some problems on Win9x (for more
info see the python FAQ).
--

Androgynous poly {*filter*} vanilla {*filter*} het    <*>     http://www.*-*-*.com/
Hugs and backrubs -- I break Rule 6

Nostalgia just ain't what it used to be



Mon, 29 Jul 2002 03:00:00 GMT  
 Redirect DOS command output
You want to look as os.popen(). This will give you a handle on the stdout
from the command that you run. If you want access to thye other std file
handles (stderr, stdin), then you will want to look at popen2() and
popen3(). The latter two functions have not been made fully portable yet in
Python 1.5.2 so if you are on Windows then you need to use
win32pipe.popen2() and win32pipe.popen3(). On UNIX they are in the os module
I think (or maybe the posix module, not sure).

In the simple case I usually do something like the following:

for line in os.popen('some command').readlines():
    # do some stuff with 'line'...

Trent

Quote:
> -----Original Message-----



> Sent: Thursday, February 10, 2000 5:08 PM

> Subject: Redirect DOS command output

> Hey, I'm using os.system() to execute a DOS command (on an NT machine).
> I would like to capture (and parse) the output from the command. I'm
> familiar with sys.stout but I'm not sure how to do this.

> Thanks for the help.

> Sent via Deja.com http://www.deja.com/
> Before you buy.
> --
> http://www.python.org/mailman/listinfo/python-list



Mon, 29 Jul 2002 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Redirecting DOS command output

2. Redirecting output of command in REXX script.

3. Redirecting command output.

4. redirect g77 under DOS 6.22- need output file

5. Redirecting DOS program output to text window

6. how to store outputs of a DOS command

7. Problem with exec command: error reading output from command: interrupted

8. problem: display command output / kill command

9. Help redirecting output from spawned process

10. Why when redirect output, it doesn;t work????

11. VSE - Redirect Standard input/output

12. Redirecting Output to files

 

 
Powered by phpBB® Forum Software