How to call a system command with flexibility on Windows

N

Nicolas Fleury

Hi everyone,
I have a hard time trying to call a system command with the following
capability on Windows:
- Redirection of stdout and stderr.
- Have the return code.

The documentation seems to say it's only possible on Unix, have I missed
something. Thx for your help.

Nicolas
 
P

Peter Hansen

Nicolas said:
I have a hard time trying to call a system command with the following
capability on Windows:
- Redirection of stdout and stderr.
- Have the return code.

The documentation seems to say it's only possible on Unix, have I missed
something. Thx for your help.

It can be done on Windows, although at the very least you cannot redirect
stderr properly from the command line on Win98.... not sure about other
issues.

I'm pretty sure return codes are available with os.system() under Windows,
but I rarely use them and forget any relevant details.

-Peter
 
F

Francis Avila

Peter Hansen wrote in message said:
It can be done on Windows, although at the very least you cannot redirect
stderr properly from the command line on Win98.... not sure about other
issues.

Win98 is particularly bad:#output snipped
0 #return codeBad command or file name
0
 
M

Mark Hammond

Nicolas said:
Hi everyone,
I have a hard time trying to call a system command with the
following capability on Windows:
- Redirection of stdout and stderr.
- Have the return code.

The documentation seems to say it's only possible on Unix, have I missed
something. Thx for your help.


Some, if not all of the popen functions on Windows will return the exit
code of the program when closing the last returned handle. I'm afraid I
have no time to check the docs or find sample code, but it does work :)

Mark.
 
M

Mark McEahern

Some, if not all of the popen functions on Windows will return the exit
code of the program when closing the last returned handle. I'm afraid I
have no time to check the docs or find sample code, but it does work :)

I believe os.popen does on both cygwin and plain windows; e.g.,

cmd = 'some command'
stdout = os.popen(cmd)
print stdout.read()
exitCode = stdout.close()
if exitCode:
print '%s failed, this probably says why:\n%s' % (cmd, stdout)

Although I suppose spelling it "stdout" is misleading--doesn't popen
return stdout/stderr combined?

Cheers,

// m
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top