473,585 Members | 2,657 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 5060
On Jul 25, 7:56*am, Emmanouil Angelakis <angel...@mpi fr-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...@mpi fr-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.w eb.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.w eb.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.w eb.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.w eb.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.w eb.dewrote:
Mike Driscoll schrieb:
On Jul 25, 7:56 am, Emmanouil Angelakis <angel...@mpi fr-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.w eb.dewrote:
>Mike Driscoll schrieb:
>>On Jul 25, 7:56 am, Emmanouil Angelakis <angel...@mpi fr-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("ente r 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\n outputname\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.Pope n, 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("o utputname2\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

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

Similar topics

10
2029
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 fairly fluent with C but it hurts to think about writing in C when Python is *so* much more appropriate for these operations. I'd like to keep my...
0
1331
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 *self, PyObject *args) { const char *command; double u0, v0, u1, v1, u2, v2, u3, v3;
3
2552
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" lib_function(....); But when I run my C++ program, I get a segmentation fault in one of the library functions (not the function I directly called but...
4
4953
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 to us by a vendor(PCMILER application). The C-Function does the following operations- Open the server,get distance between two points and close...
5
1555
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
5254
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 the IE process' activity and don't want PyQt to wait until the browser is closed. I tried the following code from within a PyQt app: import os ...
0
1785
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 import definition is located within its own class as follows: ' ' Imports System.Runtime.InteropServices '
2
5312
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: 1>make_buildinfo.obj : error LNK2019: unresolved external symbol __imp__RegQueryValueExA@24 referenced in function _make_buildinfo2 Ask on...
0
1660
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 produced by the external windows program (test.ext) within this timeout limit, I want to terminate the external windows program and continue with...
0
7908
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7836
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8199
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8336
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7950
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6606
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3835
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3863
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1447
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.