Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > passing command line arguments to executable

Reply
Thread Tools

passing command line arguments to executable

 
 
mcanjo
Guest
Posts: n/a
 
      04-03-2010
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?
 
Reply With Quote
 
 
 
 
Patrick Maupin
Guest
Posts: n/a
 
      04-03-2010
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 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?


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

Regards,
Pat
 
Reply With Quote
 
 
 
 
Simon Brunning
Guest
Posts: n/a
 
      04-03-2010
On 3 April 2010 17:09, mcanjo <> 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 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?


Have a look at the subprocess module.

--
Cheers,
Simon B.
 
Reply With Quote
 
mcanjo
Guest
Posts: n/a
 
      04-03-2010
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 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?

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



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?
 
Reply With Quote
 
Patrick Maupin
Guest
Posts: n/a
 
      04-03-2010
On Apr 3, 12:20*pm, 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 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?

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

>
> > Regards,
> > Pat

>
> 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 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

 
Reply With Quote
 
Francesco Bochicchio
Guest
Posts: n/a
 
      04-04-2010
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 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?

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

>
> > Regards,
> > Pat

>
> 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.

Ciao
--
FB

 
Reply With Quote
 
Simon Brunning
Guest
Posts: n/a
 
      04-04-2010
On 3 April 2010 18:20, mcanjo <> 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.
 
Reply With Quote
 
Gabriel Genellina
Guest
Posts: n/a
 
      04-04-2010
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
 
Reply With Quote
 
Joshua
Guest
Posts: n/a
 
      04-05-2010
On 4/3/10 12:09 PM, mcanjo 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 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?


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.
 
Reply With Quote
 
mcanjo
Guest
Posts: n/a
 
      04-05-2010
On Apr 4, 6:32*am, Simon Brunning <si...@brunningonline.net> wrote:
> 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.


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
 
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
Not Passing On Command Line Arguments Dylan Ruby 1 07-27-2009 04:32 PM
passing command line arguments thru ant jimgardener Java 1 10-20-2008 05:30 PM
Passing command line arguments to Ruby or JRuby script. Swati Sharma Ruby 5 04-07-2008 06:44 PM
Passing arguments to a command line from a python script =?iso-8859-1?q?Luis_M._Gonz=E1lez?= Python 6 03-20-2007 03:18 AM
Run Unix shell command $ parse command line arguments in python rkoida@yahoo.com Python 4 04-23-2005 04:42 AM



Advertisments