[Tutor] Running an exe from Python

Nagendra Singh singh01 at gmail.com
Wed Feb 28 18:28:14 CET 2007


Thanks a lot for all you helps. Alan your tutorial is very helpful But I
have another problem which I will post soon.

On 2/26/07, Alan Gauld <alan.gauld at btinternet.com> wrote:
>
>
> "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
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20070228/7be249df/attachment.html 


More information about the Tutor mailing list