Question: How to execute an EXE with Python?

Peter Hansen peter at engcorp.com
Tue Aug 27 07:52:41 EDT 2002


"Fausto Arinos de A. Barbuto" wrote:
> 
> Peter Hansen <peter at engcorp.com> wrote:
> > "Fausto Arinos de A. Barbuto" wrote:
> >>     I need to write a Python script to call and execute a DOS
> >>     .EXE application. 
> >
> > import os
> > os.system('c:/progs/myprog.exe')
> >
> > might do the trick.

>     Thanks for the tip, but it did not work. Sorry. :-(

Hmm, well, that may be, but it's not likely the fault of os.system(),
which does generally do just what it's supposed to do.

>     I am trying to run an executable whose source code was
>     written in C. All it does is to open a file, write integers
>     from 0 to 100 into this output file, close it and exit. A very
>     simple and prosaic test.

And presumably this program runs fine when you are at the DOS
prompt and type c:/progs/myprog.exe just like that?  Well, the
system() command is doing basically the same thing.  If you run
that command while your current directory is c:/ where does the
output file go?  Now in the same directory, after removing that
file, run Python and get the interactive prompt going.  Type the 
two commands above and check afterwards in the same location to
see if the output file was generated.  If it wasn't, there's 
likely something unusual about your configuration that you aren't
telling us.

>     Nonetheless, I get a "0" (zero) as exit status when I execute
>     the command you suggested. That means, as fas as I know,
>     that the execution had been well-succeeded. 

Check the documentation.  It tells you why you are getting a zero 
there... and it's not necessarily because it succeeded.

>     But I cannot find
>     the output file anywhere on the hard disk, however. What the
>     problem might be?

Do you have access to the source of the program, so you could
rebuild it?  Add in some print statements at the beginning and
end so you know it executed all the way through... also consider
having it print the current directory when it executes, so you
know where the file it creates is going, if it works.

I don't think you'll have any better luck with something more
complicated than os.system(), but in any event if you don't
post some more detail we're just making stabs in the dark here.

-Peter



More information about the Python-list mailing list