passing command line arguments to executable

M

mcanjo

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

Patrick Maupin

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
 
S

Simon Brunning

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

mcanjo

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

Patrick Maupin

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
 
F

Francesco Bochicchio

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
 
S

Simon Brunning

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

Gabriel Genellina

^^^^^^^^^^^^^^^^^^^^^^^^^
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 -? /?
 
J

Joshua

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

mcanjo

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?

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
 
P

Patrick Maupin

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?

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

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
 
B

Bror Johansson

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?

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top