Question: How to execute an EXE with Python?

James Kew james.kew at btinternet.com
Tue Aug 27 23:37:15 CEST 2002


"Fausto Arinos de A. Barbuto" <fbarbuto at telusplanet.net> wrote in message
news:FtRa9.7432$xc2.592187 at news0.telusplanet.net...
>
>     import os
>     myPy = 'c:/fausto/execs/teste.exe'
>     os.execl('c:/windows/command.com', '/k', myPy)

Make the slashes DOS, not Unix-style: I've found that Python is very
tolerant about paths passed to it, but these paths are getting passed to the
underlying OS and must be in the underlying OS's format.

>>> import os
>>> os.spawnl(os.P_WAIT, "C:/Windows/Calc.exe")
Traceback (most recent call last):
  File "<interactive input>", line 1, in ?
  File "C:\PYTHON22\lib\os.py", line 530, in spawnl
    return spawnv(mode, file, args)
OSError: [Errno 2] No such file or directory
>>> os.spawnl(os.P_WAIT, "C:\\Windows\\Calc.exe")
0

Running a path through os.path.normpath will give you a path compatible with
the OS:

>>> os.path.normpath("C:\\Windows\\Calc.exe")
'C:\\Windows\\Calc.exe'
>>> os.path.normpath("C:/Windows/Calc.exe")
'C:\\Windows\\Calc.exe'
>>> os.spawnl(os.P_WAIT, os.path.normpath("C:/Windows/Calc.exe"))
0

James






More information about the Python-list mailing list