Newbie question...

Carey Evans c.evans at clear.net.nz
Thu May 27 04:12:58 EDT 1999


Ken Kinder <ken at _nospam_ken.x13.com> writes:

> Good question.  Yes, there is.  Do it just how you had it.  os.system
> returns with exit code of the command you exited, in both *nix and
> Windows - that's how the C system call works, and this is just a thin
> wrapper.

Not quite.  Unix returns what /bin/sh returned, which is usually what
the called program returned.  Windows 95 returns what COMMAND.COM
returned, which is *always* zero.

I've come up with the following code, which is more likely to do what
you want.  You have to run things like "DIR" as "COMMAND.COM /C DIR"
though.


from win32process import CreateProcess, GetExitCodeProcess, STARTUPINFO, \
    CREATE_NEW_PROCESS_GROUP, NORMAL_PRIORITY_CLASS
from win32event import WaitForSingleObject, INFINITE

def system(cmd):
    pinfo = CreateProcess(
        None, cmd, None, None, 0, 0,
        None, None, STARTUPINFO())

    WaitForSingleObject(pinfo[0], INFINITE)
    return GetExitCodeProcess(pinfo[0])


-- 
	 Carey Evans  http://home.clear.net.nz/pages/c.evans/

	     "I'm not a god.  I've just been misquoted."




More information about the Python-list mailing list