faqts : Computers : Programming : Languages : Python : Common Problems

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

18 of 19 people (95%) answered Yes
Recently 10 of 10 people (100%) answered Yes

Entry

How can I call an .exe file from inside a .py file?

Jul 15th, 2000 07:33
unknown unknown, Alex Shindich, Gregoire Welraeds


Try this:
import os
os.execl ('foo.exe')
For more info read http://www.python.org/doc/current/lib/os-process.html
execl is kind of an alias for execv. So using this solution, be aware of 
the following:
execv (path, args) 
Execute the executable path with argument list args, replacing the
current process (i.e., the Python interpreter). The argument list may be 
a tuple or list of strings. Availability: Unix, Windows. 
This mean that if you have the following code in a file:
        import os
        os.execv("/bin/ls",("ls","/"))
        print "Done!" 
The last line will never be executed because the python process is 
replaced by the /bin/ls command. To avoid this, Unix allows you to use 
the fork system call :
        import os
        i= os.fork()
        if i != 0:
                os.waitpid(i,0) # wait for the child to complete.
                print done!
        else:   #  child process
                os.execv("/bin/ls",("ls","/"))
Another and easier solution, which also works under Windows :
        import os
        os.system("foo.exe")
        print 'Done!'