473,394 Members | 1,702 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Calling external program from within python

Hi,

I am tryiong to do something obviously trivial such as:
I have a c program called "tsys2list" that when it is ran it asks the user to give the value of "tcal" which is a variable. I want to call the "tsys2list" from within a pyrthon script lets call it "gamma.py" >>>but<<< I want to pass the value of "tcal" to th eprogram automatically and not by interacting with the keyboard.

How do I do that?

thanks i advance!
manolis
Jul 25 '08 #1
11 5027
On Jul 25, 7:56*am, Emmanouil Angelakis <angel...@mpifr-bonn.mpg.de>
wrote:
Hi,

I am tryiong to do something obviously trivial such as:
I have a c program called "tsys2list" that when it is ran it asks the user to give the value of "tcal" which is a variable. I want to call the *"tsys2list" from within a pyrthon script lets call it "gamma.py" >>>but<<< I want to pass the value of "tcal" to th eprogram automatically and not by interacting with the keyboard.

How do I do that?

thanks i advance!
manolis
There are probably many ways to do this. I would recommend checking
out the subprocess module and see if it does what you want. Or you
could learn a little Tkinter or wxPython and use that to get the
user's variable. Or you could even do it via the command line using
the "raw_input" command.

http://docs.python.org/lib/module-subprocess.html

Mike
Jul 25 '08 #2
Mike Driscoll schrieb:
On Jul 25, 7:56 am, Emmanouil Angelakis <angel...@mpifr-bonn.mpg.de>
wrote:
>Hi,

I am tryiong to do something obviously trivial such as:
I have a c program called "tsys2list" that when it is ran it asks the user to give the value of "tcal" which is a variable. I want to call the "tsys2list" from within a pyrthon script lets call it "gamma.py" >>>but<<< I want to pass the value of "tcal" to th eprogram automatically and not by interacting with the keyboard.

How do I do that?

thanks i advance!
manolis

There are probably many ways to do this. I would recommend checking
out the subprocess module and see if it does what you want.
This will only work if the program can be fully controlled by
commandline arguments. If interaction is required, the OP might consider
using pexpect.

Or you
could learn a little Tkinter or wxPython and use that to get the
user's variable. Or you could even do it via the command line using
the "raw_input" command.
I fail to see how *gathering* input (regardless of the method) solves
the problem of *passing* input to a subprocess.

Diez
Jul 25 '08 #3
On 2008-07-25, Diez B. Roggisch <de***@nospam.web.dewrote:
>There are probably many ways to do this. I would recommend
checking out the subprocess module and see if it does what you
want.

This will only work if the program can be fully controlled by
commandline arguments.
Why do you say that? You can interact with programs via
stdin/stdout using the subprocess module.
If interaction is required, the OP might consider using
pexpect.
Pexpect is a good option, but it's just an automation layer on
top of the same facilities that subprocess provides.

--
Grant Edwards grante Yow! I'm having a BIG BANG
at THEORY!!
visi.com
Jul 25 '08 #4
Grant Edwards schrieb:
On 2008-07-25, Diez B. Roggisch <de***@nospam.web.dewrote:
>>There are probably many ways to do this. I would recommend
checking out the subprocess module and see if it does what you
want.
This will only work if the program can be fully controlled by
commandline arguments.

Why do you say that? You can interact with programs via
stdin/stdout using the subprocess module.

Because usually if a program *prompts* the user to enter input (and that
was what I read from the OP's post), one has to deal with pseudo
terminals, not with stdin/out.
>If interaction is required, the OP might consider using
pexpect.

Pexpect is a good option, but it's just an automation layer on
top of the same facilities that subprocess provides.
AFAIK it's more than that. I'm not an expert on pseudo terminals, but
AFAIK setting using module pty isn't possible using subprocess.

Diez
Jul 25 '08 #5
oj
On Jul 25, 3:44*pm, "Diez B. Roggisch" <de...@nospam.web.dewrote:
Because usually if a program *prompts* the user to enter input (and that
was what I read from the OP's post), one has to deal with pseudo
terminals, not with stdin/out.
How does the program writing some text before taking input change how
it takes input?

If it runs in a terminal and takes input from the user via keyboard,
as in the user types a response and presses enter, it's most likely
using stdin, in which case subprocess would be perfect.

Perhaps that OP can clarify how the tcal program takes input?
Jul 25 '08 #6
oj schrieb:
On Jul 25, 3:44 pm, "Diez B. Roggisch" <de...@nospam.web.dewrote:
>Because usually if a program *prompts* the user to enter input (and that
was what I read from the OP's post), one has to deal with pseudo
terminals, not with stdin/out.

How does the program writing some text before taking input change how
it takes input?

If it runs in a terminal and takes input from the user via keyboard,
as in the user types a response and presses enter, it's most likely
using stdin, in which case subprocess would be perfect.
Try using SSH with subprocess and prepare to be disappointed when it
asks for a password.

See this for a discussion:

http://mail.python.org/pipermail/dis...ay/007553.html
Diez

Jul 25 '08 #7
On Jul 25, 9:28*am, "Diez B. Roggisch" <de...@nospam.web.dewrote:
Mike Driscoll schrieb:
On Jul 25, 7:56 am, Emmanouil Angelakis <angel...@mpifr-bonn.mpg.de>
wrote:
Hi,
I am tryiong to do something obviously trivial such as:
I have a c program called "tsys2list" that when it is ran it asks the user to give the value of "tcal" which is a variable. I want to call the *"tsys2list" from within a pyrthon script lets call it "gamma.py" >>>but<<< I want to pass the value of "tcal" to th eprogram automatically and not by interacting with the keyboard.
How do I do that?
thanks i advance!
manolis
There are probably many ways to do this. I would recommend checking
out the subprocess module and see if it does what you want.

This will only work if the program can be fully controlled by
commandline arguments. If interaction is required, the OP might consider
using pexpect.
Or you
could learn a little Tkinter or wxPython and use that to get the
user's variable. Or you could even do it via the command line using
the "raw_input" command.

I fail to see how *gathering* input (regardless of the method) solves
the problem of *passing* input to a subprocess.

Diez
My understanding of the OP's request was that "tsys2list" was a custom
script of some sort that gathered info from the user, so I wanted to
know why he didn't just skip calling it and write something in Tkinter
or wxPython or even via the CLI instead.

I'm probably just not understanding something.

Mike
Jul 25 '08 #8
Mike Driscoll schrieb:
On Jul 25, 9:28 am, "Diez B. Roggisch" <de...@nospam.web.dewrote:
>Mike Driscoll schrieb:
>>On Jul 25, 7:56 am, Emmanouil Angelakis <angel...@mpifr-bonn.mpg.de>
wrote:
Hi,
I am tryiong to do something obviously trivial such as:
I have a c program called "tsys2list" that when it is ran it asks the user to give the value of "tcal" which is a variable. I want to call the "tsys2list" from within a pyrthon script lets call it "gamma.py" >>>but<<< I want to pass the value of "tcal" to th eprogram automatically and not by interacting with the keyboard.
How do I do that?
thanks i advance!
manolis
There are probably many ways to do this. I would recommend checking
out the subprocess module and see if it does what you want.
This will only work if the program can be fully controlled by
commandline arguments. If interaction is required, the OP might consider
using pexpect.
>>Or you
could learn a little Tkinter or wxPython and use that to get the
user's variable. Or you could even do it via the command line using
the "raw_input" command.
I fail to see how *gathering* input (regardless of the method) solves
the problem of *passing* input to a subprocess.

Diez

My understanding of the OP's request was that "tsys2list" was a custom
script of some sort that gathered info from the user, so I wanted to
know why he didn't just skip calling it and write something in Tkinter
or wxPython or even via the CLI instead.

I'm probably just not understanding something.
The program is doing some work, and needs to be told what actually to
do. It does so by asking the user, sure. But then it *works*. I think
that's pretty clear from

"""
I have a c program called "tsys2list" that when it is ran it asks the
user to give the value of "tcal" which is a variable. I want to call the
"tsys2list" from within a pyrthon script lets call it "gamma.py"
>>>but<<< I want to pass the value of "tcal" to th eprogram
automatically and not by interacting with the keyboard.
"""

So replacing it with something that asks the same questions leaves us
with the work undone...
Diez
Jul 25 '08 #9
These answers are too elaborate and abstract for the question.

Emmanouil,

Here is a program "myprog" which takes input and writes output to a
file. It happens to be python but it could be anything.

#####
#!/usr/bin/env python
a = int(raw_input("enter thing 1 "))
b = int(raw_input("enter thing 2 "))
c = raw_input("enter output file name ")
f = open(c,"w")
f.write("answer is %s\n" % str(a + b))
f.close()
#####

Here is python code which runs that program

#####
import os

f = os.popen("/path/to/myprog","w")
f.write("3\n4\noutputname\n") #can also do three separate writes if
that is more convenient
f.close()
#####

For some reason os.popen is deprecated in favor of the more verbose
subprocess.Popen, but this will work for a while.

Here is an even simpler approach that will take you a long way. This
should be very easy to understand.

#####
import os

params = open("temp","w")
params.write("3\n")
params.write("4\n")
params.write("outputname2\n")
params.close()

os.system("/path/to/myprog < temp")
#####

If you want python to pick up the stdout of your program as well look
into popen2.

Try basing your solution on those and if you have any problems let us
know what they are. In most cases such as you describe these will
work.

mt

Jul 25 '08 #10
On 2008-07-25, Diez B. Roggisch <de***@nospam.web.dewrote:
Because usually if a program *prompts* the user to enter input (and that
was what I read from the OP's post), one has to deal with pseudo
terminals, not with stdin/out.
>>If interaction is required, the OP might consider using
pexpect.

Pexpect is a good option, but it's just an automation layer on
top of the same facilities that subprocess provides.

AFAIK it's more than that. I'm not an expert on pseudo terminals, but
AFAIK setting using module pty isn't possible using subprocess.
You're right. I forgot that pexect uses a pty. You could use
a pty with the subprocess module, but it's a hassle to set up.

--
Grant Edwards grante Yow! The PINK SOCKS were
at ORIGINALLY from 1952!!
visi.com But they went to MARS
around 1953!!
Jul 25 '08 #11


Michael Tobis wrote:
For some reason os.popen is deprecated in favor of the more verbose
subprocess.Popen, but this will work for a while.
As explained in
http://www.python.org/dev/peps/pep-0324/
subprocess consolidated replaced several modules and functions (popen*,
system, spawn*, ???)with better security, exception handling, and
flexibility. The deprecated modules are gone in 3.0, so the OP might
want to start with subprocess now.

Jul 25 '08 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Kyler Laird | last post by:
I need to submit C/C++ code for a class. (It's not a programming class. The choice of language is inertial. I think that it mostly serves to distract students from the course subject.) I'm...
0
by: Praveen, Tayal (IE10) | last post by:
Hi Guys, I am having problems in the following C API program where myOtim_system is callable from python function. The module is listed below - static PyObject * myOptim_system(PyObject...
3
by: Glenn C. Rhoads | last post by:
I'm having a problem calling a C function from a publicly available library file. To get the code to compile, I had to declare the function as an external C function as follows. extern "C"...
4
by: Sivakumar Shanmugam | last post by:
Group.. We are running on db2 UDF V8 on Sun solaris platform. I created an UDF which calles a C-routine(SQL_API_FN). This C-routine in turn calls an external C-function. The C-function is provided...
5
by: ash | last post by:
hi, i want to know is there a way to run/control an external program form within a python program? thanks in advance for any support.
4
by: gregarican | last post by:
What's the easiest and cleanest way of having PyQt bring up an external application? In this case I am looking to launch Internet Explorer and bring up a specific URL. I don't care about tracking...
0
by: Michael | last post by:
Trying to call an external DLL (compiled within Fortran) and keep on getting the following error in the main call to the external DLL ' Object reference not set to instance of object' The DLL...
2
by: f rom | last post by:
----- Forwarded Message ---- From: Josiah Carlson <jcarlson@uci.edu> To: f rom <etaoinbe@yahoo.com>; wxpython-users@lists.wxwidgets.org Sent: Monday, December 4, 2006 10:03:28 PM Subject: Re: ...
0
by: Chico Jayanthan | last post by:
Hi, I call an external windows program (.exe) from a python script. Something like os.system('test.exe'). I want to set a timeout for the external windows program to return. If no results are...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.