Question: How to execute an EXE with Python?

James Kew james.kew at
Tue Aug 27 17:37:15 EDT 2002

"Fausto Arinos de A. Barbuto" <fbarbuto at> wrote in message
news:FtRa9.7432$xc2.592187 at
>     import os
>     myPy = 'c:/fausto/execs/teste.exe'
>     os.execl('c:/windows/', '/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\", 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")

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

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


More information about the Python-list mailing list