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

passing command line arguments to executable

11 views
Skip to first unread message

mcanjo

unread,
Apr 3, 2010, 12:09:52 PM4/3/10
to
I have an executable (I don't have access to the source code) that
processes some data. I double click on the icon and a Command prompt
window pops up. The program asks me for the input file, I hit enter,
and then it asks me for and output filename, I hit enter a second time
and it goes off and does its thing and when it is finished running the
Command Prompt goes away and I have my new output file in the same
directory as my executable and input file. I would like to be able to
batch process a group of files. I thought about using "os.spawnv()" in
a loop and at each iteration of the loop passing in the file in and
out names but that didn't work. Does anyone have any ideas?

Patrick Maupin

unread,
Apr 3, 2010, 12:15:26 PM4/3/10
to

You need to look at the subprocess module, and use pipes.

Regards,
Pat

Simon Brunning

unread,
Apr 3, 2010, 12:22:31 PM4/3/10
to mcanjo, python-list

Have a look at the subprocess module.

--
Cheers,
Simon B.

mcanjo

unread,
Apr 3, 2010, 1:20:44 PM4/3/10
to


I tried doing the following code:

from subprocess import Popen
from subprocess import PIPE, STDOUT
exefile = Popen('pmm.exe', stdout = PIPE, stdin = PIPE, stderr =
STDOUT)
exefile.communicate('MarchScreen.pmm\nMarchScreen.out')[0]

and the Command Prompt opened and closed, no exceptions were generated
but the program didn't run. Am I doing something wrong?

Patrick Maupin

unread,
Apr 3, 2010, 1:29:49 PM4/3/10
to

I don't use communicate because I've never gotten it to do what I
want. I also don't use Windows. I have a linux solution that allows
me to feed stuf into a pipe, but I don't think it will work under
Windows because it uses os functions to block on empty pipes that
Windows doesn't support.

You might read PEP 3145 -- it addresses some of the issues, and there
is some code to help, I think:

http://www.python.org/dev/peps/pep-3145/

Regards,
Pat

Francesco Bochicchio

unread,
Apr 4, 2010, 5:17:59 AM4/4/10
to

I would try a couple of things (never done what you are trying to do,
so my suggestions may be useless ):
1. use shell=True as parameter of Popen
2. capture the output of communicate method, which returns whatever
the process emits on standard output and standard error: there could
be some message that give you hints about the solution.

Ciao
--
FB

Simon Brunning

unread,
Apr 4, 2010, 7:32:33 AM4/4/10
to python-list
On 3 April 2010 18:20, mcanjo <mca...@gmail.com> wrote:
> I tried doing the following code:
>
> from subprocess import Popen
> from subprocess import PIPE, STDOUT
> exefile = Popen('pmm.exe', stdout = PIPE, stdin = PIPE, stderr =
> STDOUT)
> exefile.communicate('MarchScreen.pmm\nMarchScreen.out')[0]
>
> and the Command Prompt opened and closed, no exceptions were generated
> but the program didn't run. Am I doing something wrong?

Have you tried running pmm.exe from the command line? What does that
look like? Does it matter what the current working directory is at the
time?

--
Cheers,
Simon B.

Gabriel Genellina

unread,
Apr 4, 2010, 6:23:07 PM4/4/10
to
On 4 abr, 06:17, Francesco Bochicchio <bieff...@gmail.com> wrote:
> On 3 Apr, 19:20, mcanjo <mca...@gmail.com> wrote:
> > On Apr 3, 11:15 am, Patrick Maupin <pmau...@gmail.com> wrote:
> > > On Apr 3, 11:09 am, mcanjo <mca...@gmail.com> wrote:
>
> > > > I have an executable (I don't have access to the source code) that
> > > > processes some data. I double click on the icon and a Command prompt
> > > > window pops up. The program asks me for the input file, I hit enter,
> > > > and then it asks me for and output filename, I hit enter a second time

^^^^^^^^^^^^^^^^^^^^^^^^^
> > > > and it goes off and does its thing and when it is finished ...

>
> > > You need to look at the subprocess module, and use pipes.
>
> > I tried doing the following code:
>
> > from subprocess import Popen
> > from subprocess import PIPE, STDOUT
> > exefile = Popen('pmm.exe', stdout = PIPE, stdin = PIPE, stderr =
> > STDOUT)
> > exefile.communicate('MarchScreen.pmm\nMarchScreen.out')[0]
>
> > and the Command Prompt opened and closed, no exceptions were generated
> > but the program didn't run. Am I doing something wrong?
>
> I would try a couple of things (never done what you are trying to do,
> so my suggestions may be useless ):
> 1. use shell=True as parameter of Popen
> 2. capture the output of communicate method, which returns whatever
> the process emits on standard output and standard error: there could
> be some message that give you hints about the solution.

To mcanjo: note that you didn't provide the second '\n'

Also, are you sure the program does not accept command line arguments?
Many do, and switch to interactive mode when no argument is provided.
I'd try with -h /h --help /help -? /?

--
Gabriel Genellina

Joshua

unread,
Apr 4, 2010, 9:47:01 PM4/4/10
to

I've done this a couple times on Windows. There are a few ways to do it,
but I've found the easiest way is to use subprocess.check_call(). If you
need to build the call with variables I find it cleaner to build the
string before calling check_call(). Below is an example.

callString = 'program.exe -x ' + variable
returnMessage = subprocess.check_call(callString, shell=True)

This works for programs that take arguments, I don't know how it will
work with the program you have that asks for input in an interactive
way. Does it accept arguments when you first run it? Not sure how to do
it otherwise.

-Josh B.

mcanjo

unread,
Apr 5, 2010, 12:22:12 PM4/5/10
to

When I run the program from the command line it looks as follows:

Enter the Input filename
(enter in filename here)
Enter the Output filename
(enter in filename here)

If an absolute path is not specified then the output file is located
in the current working directory of the executable. The absolute path
for the output and input files may be specified also.

Chris

Patrick Maupin

unread,
Apr 5, 2010, 12:31:01 PM4/5/10
to

One thing you should do if you use pipes is to make sure you are
accepting data from the program. If the program stalls because it
cannot write anything to its stdout, you might have an issue.

Pat

Bror Johansson

unread,
Apr 6, 2010, 4:15:23 PM4/6/10
to

It's been quite a while since I had to solve problems like the one you
have. Sometimes I used a Python implementation of 'expect' successfully.

0 new messages