Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

calling a program from Python batch file

53 views
Skip to first unread message

Karim Farokhnia

unread,
Dec 7, 2016, 11:39:01 AM12/7/16
to
Hi there,

I am writing a batch file in Python. The batch file, in part, calls a program named "oq-console.bat" to run. Then once the program comes up (it looks like windows CMD), I need the batch file to type some commands to make it run (just like I would do it by myself).

I need the batch file does the whole things automatically, I mean calls the program, types in commands and press enter!

So far, I have used the below Python codes in my batch file to do that;


import subprocess

subprocess.call(["C:\Program Files (x86)\OpenQuake Engine\oq-console.bat", "oq --run", "job.ini"])


Note that the first string is the program, and the last two are the commands which needs to be typed in.

It pulls up the program well, but doesn't type in the commands to make it to run.


Any ideas on how I can get the program to run the commands as well?

Thank you!

Michael Torrie

unread,
Dec 7, 2016, 12:18:51 PM12/7/16
to
On 12/07/2016 09:38 AM, Karim Farokhnia wrote:
> I am writing a batch file in Python. The batch file, in part, calls a
> program named "oq-console.bat" to run. Then once the program comes up
> (it looks like windows CMD), I need the batch file to type some
> commands to make it run (just like I would do it by myself).
>
> I need the batch file does the whole things automatically, I mean
> calls the program, types in commands and press enter!

Is the program that oq-console.bat runs interactive? After it launches
into its own console window it asks for more input?

> So far, I have used the below Python codes in my batch file to do
> that;
>
>
> import subprocess
>
> subprocess.call(["C:\Program Files (x86)\OpenQuake
> Engine\oq-console.bat", "oq --run", "job.ini"])
>
>
> Note that the first string is the program, and the last two are the
> commands which needs to be typed in.
>
> It pulls up the program well, but doesn't type in the commands to
> make it to run.

You'll have to explain what you mean by "[it] doesn't type ni the
commands to make it run."

Bob Gailer

unread,
Dec 7, 2016, 12:31:48 PM12/7/16
to
On Dec 7, 2016 11:40 AM, "Karim Farokhnia" <karim.f...@gmail.com>
wrote:
>
> Hi there,
>
> I am writing a batch file in Python. The batch file, in part, calls a
program named "oq-console.bat" to run. Then once the program comes up (it
looks like windows CMD), I need the batch file to type some commands to
make it run (just like I would do it by myself).
>
> I need the batch file does the whole things automatically, I mean calls
the program, types in commands and press enter!

Try using the stdin argument of subprocess.
>
> So far, I have used the below Python codes in my batch file to do that;
>
>
> import subprocess
>
> subprocess.call(["C:\Program Files (x86)\OpenQuake
Engine\oq-console.bat", "oq --run", "job.ini"])
>
>
> Note that the first string is the program, and the last two are the
commands which needs to be typed in.
>
> It pulls up the program well, but doesn't type in the commands to make it
to run.
>
>
> Any ideas on how I can get the program to run the commands as well?
>
> Thank you!
> --
> https://mail.python.org/mailman/listinfo/python-list

Karim Farokhnia

unread,
Dec 7, 2016, 12:43:27 PM12/7/16
to
On Wednesday, December 7, 2016 at 12:18:51 PM UTC-5, Michael Torrie wrote:
> On 12/07/2016 09:38 AM, Karim Farokhnia wrote:
> > I am writing a batch file in Python. The batch file, in part, calls a
> > program named "oq-console.bat" to run. Then once the program comes up
> > (it looks like windows CMD), I need the batch file to type some
> > commands to make it run (just like I would do it by myself).
> >
> > I need the batch file does the whole things automatically, I mean
> > calls the program, types in commands and press enter!
>
> Is the program that oq-console.bat runs interactive? After it launches
> into its own console window it asks for more input?
The program doesn't ask for more input parameters as all parameters are in input files and the program will automatically read the files, but I need to type in some required commands and press enter.

>
> > So far, I have used the below Python codes in my batch file to do
> > that;
> >
> >
> > import subprocess
> >
> > subprocess.call(["C:\Program Files (x86)\OpenQuake
> > Engine\oq-console.bat", "oq --run", "job.ini"])
> >
> >
> > Note that the first string is the program, and the last two are the
> > commands which needs to be typed in.
> >
> > It pulls up the program well, but doesn't type in the commands to
> > make it to run.
>
> You'll have to explain what you mean by "[it] doesn't type ni the
> commands to make it run."

By [it] I meant the program.

Michael Torrie

unread,
Dec 7, 2016, 12:58:57 PM12/7/16
to
On 12/07/2016 10:43 AM, Karim Farokhnia wrote:
>> Is the program that oq-console.bat runs interactive? After it
>> launches into its own console window it asks for more input?
> The program doesn't ask for more input parameters as all parameters
> are in input files and the program will automatically read the files,
> but I need to type in some required commands and press enter.

think what you mean is that it *is* interactive, at least requiring some
input before it begins to work.

As the other person who replied said, it sounds like this program will
accept input on standard in. From python you will probably have to use
subprocess.popen and use the .communicate() method. You will be able to
pass in a string that will get fed to the subprocess on it's standard in
pipe (the equivalent of typing and pressing enter). Check the online
docs, particularly for the .communicate() method.

John Gordon

unread,
Dec 7, 2016, 12:59:50 PM12/7/16
to
In <f9d6e83d-d8e7-4245...@googlegroups.com> Karim Farokhnia <karim.f...@gmail.com> writes:

> Hi there,

> I am writing a batch file in Python. The batch file, in part, calls a
> program named "oq-console.bat" to run. Then once the program comes up
> (it looks like windows CMD), I need the batch file to type some commands
> to make it run (just like I would do it by myself).

If you need to start up a program, provide interactive input to it, and
perhaps examine its interactive output, then you want the "pexpect" module:

Pexpect is a pure Python module for spawning child applications;
controlling them; and responding to expected patterns in their output.
Pexpect allows your script to spawn a child application and control it
as if a human were typing commands.

https://pexpect.readthedocs.io/en/stable/

--
John Gordon A is for Amy, who fell down the stairs
gor...@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

Michael Torrie

unread,
Dec 7, 2016, 1:23:23 PM12/7/16
to
On 12/07/2016 10:59 AM, John Gordon wrote:
> In <f9d6e83d-d8e7-4245...@googlegroups.com> Karim Farokhnia <karim.f...@gmail.com> writes:
>
>> Hi there,
>
>> I am writing a batch file in Python. The batch file, in part, calls a
>> program named "oq-console.bat" to run. Then once the program comes up
>> (it looks like windows CMD), I need the batch file to type some commands
>> to make it run (just like I would do it by myself).
>
> If you need to start up a program, provide interactive input to it, and
> perhaps examine its interactive output, then you want the "pexpect" module:
>
> Pexpect is a pure Python module for spawning child applications;
> controlling them; and responding to expected patterns in their output.
> Pexpect allows your script to spawn a child application and control it
> as if a human were typing commands.
>
> https://pexpect.readthedocs.io/en/stable/

Does Pexpect work on Windows?

In the OP's case it looks like the standard in pipe is sufficient.

Stephen Tucker

unread,
Dec 7, 2016, 2:56:02 PM12/7/16
to
This might be totally irrelevant, but, if (a) the data to be read by the
program during a given run is known when the program is run/launched, (b)
that data is purely textual and (c) that data can be read by the program
from the stdin channel, then my idea is (1) before the launch, put that
data into a file named, say, inputfile.dat, and then (2) launch the
program with a command like

call program.bat <inputfile.dat

Moreover, if the program needs to output textual data, and can do so using
stdout, then the data can be sent to another file named, say,
outputfile.dat as follows:

call program.bat <inputfile.dat >outputfile.dat

Finally, if the program can read its data from a given file and possibly
needs to output its data, again, to a named file, and those files' names
can be read from the parameters to the call, then

call program.bat inputfile.dat outputfile.dat

can be used.








On Wed, Dec 7, 2016 at 6:23 PM, Michael Torrie <tor...@gmail.com> wrote:

> On 12/07/2016 10:59 AM, John Gordon wrote:
> > In <f9d6e83d-d8e7-4245...@googlegroups.com> Karim
> Farokhnia <karim.f...@gmail.com> writes:
> >
> >> Hi there,
> >
> >> I am writing a batch file in Python. The batch file, in part, calls a
> >> program named "oq-console.bat" to run. Then once the program comes up
> >> (it looks like windows CMD), I need the batch file to type some commands
> >> to make it run (just like I would do it by myself).
> >
> > If you need to start up a program, provide interactive input to it, and
> > perhaps examine its interactive output, then you want the "pexpect"
> module:
> >
> > Pexpect is a pure Python module for spawning child applications;
> > controlling them; and responding to expected patterns in their
> output.
> > Pexpect allows your script to spawn a child application and control
> it
> > as if a human were typing commands.
> >
> > https://pexpect.readthedocs.io/en/stable/
>
> Does Pexpect work on Windows?
>
> In the OP's case it looks like the standard in pipe is sufficient.
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>

Paul Moore

unread,
Dec 8, 2016, 11:42:06 AM12/8/16
to
On Wednesday, 7 December 2016 18:23:23 UTC, Michael Torrie wrote:

> Does Pexpect work on Windows?

Apparently yes: https://pexpect.readthedocs.io/en/stable/overview.html#pexpect-on-windows

"New in version 4.0: Windows support"

> In the OP's case it looks like the standard in pipe is sufficient.

Agreed.
Paul
0 new messages