Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Running External Programs from Within Python

Reply
Thread Tools

Running External Programs from Within Python

 
 
Bob=Moore
Guest
Posts: n/a
 
      01-31-2004
I'm considering making the transfer from Rexx to Python as a scripting
language, but there's one thing I can do in Rexx that I can't seem to
do in Python: run an external program.

Suppose I have a Tkinter GUI and I want to just push a button and run
the Windows utility notepad.exe. Or push another button and run one
of my old Rexx programs.

Basically I want to send a command line to Python and have it run that
command line as if I'm sending it to the Windows command-prompt, an
MS-DOS window, or clicking on an appropriate program icon.

Can I run (call? exec? eval?) an external program from inside a Python
program?

I have checked the FAQ and documentation.

Thanx,

Bob=Moore
 
Reply With Quote
 
 
 
 
Dirk Hagemann
Guest
Posts: n/a
 
      01-31-2004
Bob=Moore wrote:
> I'm considering making the transfer from Rexx to Python as a scripting
> language, but there's one thing I can do in Rexx that I can't seem to
> do in Python: run an external program.
>
> Suppose I have a Tkinter GUI and I want to just push a button and run
> the Windows utility notepad.exe. Or push another button and run one
> of my old Rexx programs.
>
> Basically I want to send a command line to Python and have it run that
> command line as if I'm sending it to the Windows command-prompt, an
> MS-DOS window, or clicking on an appropriate program icon.
>
> Can I run (call? exec? eval?) an external program from inside a Python
> program?
>
> I have checked the FAQ and documentation.
>
> Thanx,
>
> Bob=Moore


Hi Bob!

That is very easy: popen()
Check the FAQ and documentation again for popen.

Regards
Dirk
 
Reply With Quote
 
 
 
 
RayS
Guest
Posts: n/a
 
      01-31-2004
Hi Dirk,

>That is very easy: popen()
>Check the FAQ and documentation again for popen.


That reminds me, I was asked if you can open a bi-directional pipe to
control an external console program on Windows. A guy has compiled FORTRAN
(~4,000 lines of 1980's spaghetti) and wants to add a modern GUI and a
front end method to check data.
Can you both send commands to, and read STDOUT from, a remote console app?

Ray


 
Reply With Quote
 
=?iso-8859-15?Q?David_D=FCrrenmatt?=
Guest
Posts: n/a
 
      01-31-2004
On 31 Jan 2004 11:23:42 -0800, Bob=Moore <(E-Mail Removed)> wrote:

> Can I run (call? exec? eval?) an external program from inside a Python
> program?


Try this:

import os
os.system('notepad.exe') # starts notepad

For complex interaction, use win32pipe (Win32 Extensions).


dave


 
Reply With Quote
 
Nuff Said
Guest
Posts: n/a
 
      01-31-2004
On Sat, 31 Jan 2004 11:23:42 -0800, Bob=Moore wrote:

>
> Can I run (call? exec? eval?) an external program from inside a Python
> program?
>


Check out os.popen (in all it's variants); e.g. something like
the following will do what you want:

import os

stdin, stdout, stderr = os.popen3('your program goes here')
output = stdout.read()
errors = stderr.read()
stdin.close(); stdout.close(); stderr.close()

... do something with 'output' and 'errors' ...

HTH / Nuff

 
Reply With Quote
 
Dirk Hagemann
Guest
Posts: n/a
 
      01-31-2004
RayS wrote:
> Hi Dirk,
>
>> That is very easy: popen()
>> Check the FAQ and documentation again for popen.

>
>
> That reminds me, I was asked if you can open a bi-directional pipe to
> control an external console program on Windows. A guy has compiled
> FORTRAN (~4,000 lines of 1980's spaghetti) and wants to add a modern GUI
> and a front end method to check data.
> Can you both send commands to, and read STDOUT from, a remote console app?
>
> Ray
>
>

I'm not sure if I got the point, but, for example, with popen I can ping
from the console a computer and read the result.
But sending commands to a remote console is a problem which troubles
myself in another context (I asked here yesterday).

Dirk
 
Reply With Quote
 
Cameron Laird
Guest
Posts: n/a
 
      02-01-2004
In article <bvhbr7$6rj$07$(E-Mail Removed)-online.com>,
Dirk Hagemann <(E-Mail Removed)> wrote:
>RayS wrote:
>> Hi Dirk,
>>
>>> That is very easy: popen()
>>> Check the FAQ and documentation again for popen.

>>
>>
>> That reminds me, I was asked if you can open a bi-directional pipe to
>> control an external console program on Windows. A guy has compiled
>> FORTRAN (~4,000 lines of 1980's spaghetti) and wants to add a modern GUI
>> and a front end method to check data.
>> Can you both send commands to, and read STDOUT from, a remote console app?
>>
>> Ray
>>
>>

>I'm not sure if I got the point, but, for example, with popen I can ping
>from the console a computer and read the result.
>But sending commands to a remote console is a problem which troubles
>myself in another context (I asked here yesterday).
>
>Dirk


The answer is, yes, you can do what you want--assuming that
"remote" means something different to you than the way the
rest of us are reading it.

The kind of wrapping you're describing is a VERY common use
of Python. Before we provide more details, though, you go
first; specifically, are you thinking of your bi-directional
pipe as a batch operation, or a programmatic one? Do you
know at the time you launch the Fortran-coded application
all the input it'll need?
--

Cameron Laird <(E-Mail Removed)>
Business: http://www.Phaseit.net
 
Reply With Quote
 
RayS
Guest
Posts: n/a
 
      02-01-2004
Hi Cameron,

>Before we provide more details, though, you go
>first; specifically, are you thinking of your bi-directional
>pipe as a batch operation, or a programmatic one?


Interactive/programmatic...

>Do you
>know at the time you launch the Fortran-coded application
>all the input it'll need?


Usually not. It's a balance solution routine that requires some interaction
from the engineers; the Fortran has a three layer menu, and produces both
screen output and files. There is an intermediary result which has to be
looked at by the user to decide on the final balance, otherwise I would
change the code to just run from arg[] values.
One of its problems is that there is minimal error checking, and a bad
input requires the user to start over!
From the popen() docs, it seemed as though Windows could not have both
read and write ability over a pipe.

Ray


 
Reply With Quote
 
Cameron Laird
Guest
Posts: n/a
 
      02-01-2004
In article <(E-Mail Removed)>,
RayS <(E-Mail Removed)> wrote:
.
.
.
>>Do you
>>know at the time you launch the Fortran-coded application
>>all the input it'll need?

>
>Usually not. It's a balance solution routine that requires some interaction
>from the engineers; the Fortran has a three layer menu, and produces both
>screen output and files. There is an intermediary result which has to be
>looked at by the user to decide on the final balance, otherwise I would
>change the code to just run from arg[] values.
>One of its problems is that there is minimal error checking, and a bad
>input requires the user to start over!
> From the popen() docs, it seemed as though Windows could not have both
>read and write ability over a pipe.

.
.
.
Right. What you probably want is os.popen2(), which
<URL: http://python.org/doc/current/lib/module-popen2.html >
documents. popen() is *not* adequate to your situation.

I write "probably" above only because your description doesn't
make clear to me whether you need your Fortran process's stderr.
If you do, then read through that same reference page to study
popen3() and popen4().

Keep in mind that you're doing something at which Python is
quite apt. This kind of wrapping can be fun. You'll be suc-
cessful.
--

Cameron Laird <(E-Mail Removed)>
Business: http://www.Phaseit.net
 
Reply With Quote
 
Ray S
Guest
Posts: n/a
 
      09-21-2004
Does anyone know why a script that uses win32pipe.popen3() will run a
script, but hang when compiled?

I can't get a script that uses pipedream.py to run when compiled with
McMillan or py2exe; it appears that the client process (an interactive DOS
exe) returns the first screen of text, gets to its first prompt, then exits.

pipedream.py does: c = os.read(self._in.fileno(), 1) and just hangs, no error.

I'd appreciate any and all tips ...

Ray Schumacher
Blue Cove Interactive
7220 Trade Street, Suite 101
San Diego, CA 92121
858.695.8801
http://Blue-Cove.com

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing string from python programs to external programs lone_eagle Python 3 05-26-2009 09:16 PM
Calling external programs from within a Tomcat application pwaring@gmail.com Java 7 07-29-2008 11:45 AM
Re: running programs from within python M.N.A.Smadi Python 6 03-09-2005 08:17 PM
running programs from within python M.N.A.Smadi Python 0 03-09-2005 04:51 PM
How to Verify Programs Running with Installed Programs? Edge Computer Information 9 01-19-2004 11:35 PM



Advertisments