[Tutor] Running an exe from Python

Alan Gauld alan.gauld at btinternet.com
Mon Feb 26 19:31:36 CET 2007


"Nagendra Singh" <singh01 at gmail.com> wrote

> Thanks a lot for all the suggestions. I used the function
> subprocess.call ( 'c:\abc.exe  c:\data\file1'), but as before
> the command window opens and closes very fast
> a value of 1 is displayed. How do I see the results??

The result is 1 which indicates an error. You don't want
the result you want the outpur, which is a different thing
entirely! :-)

To get the output you need to access the output stream
of the process which is usually stdout. The old way to do
that was with os.popen, but the subprocess module
provides a new way. The m,odule docs describe how
to replace popen using subprocess' Popen class.

My tutorial shows an example of the same thing based
on the odule documentation.

Basically it looks like:

import subprocess
psout = subprocess.Popen(r'c:\abc.exe c:\data\file1', 
shell=True, stdout=PIPE).stdout
results = psout.read().split('\n')
Notice I enclosed the command with a raw string.Otherwise your 
backslashes get treated as escape characters. This might be why you 
are getting error codes back?Another way to avoid that is to use 
forward slashes which Python understands on DOS psout = 
subprocess.Popen('c:/abc.exe c:/data/file1', 
shell=True, stdout=PIPE).stdout
> I am sorry if I sound dumb.

Nope, just looking for the wrong thing. But you only
know that after you find out :-)

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 




More information about the Tutor mailing list