Python Forum

Full Version: How to pass args to myprog.exe
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I've Googled this but it's been a long day and maybe some kind soul will share some simple example code so I can get out of this chair for today :-)

I need simple examples of launching an external .exe file and passing it an argument or arguments?

I think you use subprocess and pOpen but I've yet to find a simple example for say the following...

If run from the command line...

# example 1 - launch a program and pass it some text as an argument
> myprog.exe arg1

# example 2 - launch a program and pass it a file
> vlc test.wav

# example 3 - launch a program and pass it some arguments and get the results
> set /a 2+3
> 5

How to do this from Python?

thanks for any help
You can do this in two ways. sys module or argparse module

sys.argv is a list of all arguments given with the first as the name of the script
So the first argument would be the second element via

filename.py
import sys
print(sys.argv[1])
in the command prompt/terminal
python test3.py metul
would result in
Output:
metul
However giving arguments can get complicated. Which is where argparse comes into play. Its a module to handle complex arguments.

A simple true or false value would be like

import argparse

parser = argparse.ArgumentParser(description='metulburr\'s Arguments')
parser.add_argument('-c','--clean', action='store_true', help='helpful info in the help')
args = vars(parser.parse_args())

print(args)
with the commands and output as...
Output:
metulburr@ubuntu:~$ python test3.py {'clean': False} metulburr@ubuntu:~$ python test3.py -c {'clean': True} metulburr@ubuntu:~$ python test3.py -h usage: test3.py [-h] [-c] metulburr's Arguments optional arguments: -h, --help show this help message and exit -c, --clean helpful info in the help
You can do a lot more than just true/false values. You can do metavars, type, various number of arguments (nargs), easily add/remove arguments, etc. Here is an example of a game we made using this
import argparse

parser = argparse.ArgumentParser(description='Program Arguments')
parser.add_argument('-c','--center', action='store_false',
    help='position starting window at (0,0), sets SDL_VIDEO_CENTERED to false')
parser.add_argument('-w','--winpos', nargs=2, default=(0,0), metavar=('X', 'Y'),
    help='position starting window at (X,Y), default is (0,0)')
parser.add_argument('-s' , '--size', nargs=2, default=(800,600), metavar=('WIDTH', 'HEIGHT'),
    help='set window size to WIDTH HEIGHT, defualt is {}'.format((800,600)))
parser.add_argument('-f' , '--fullscreen', action='store_true',
    help='start in fullscreen')
parser.add_argument('-m' , '--music_off', action='store_true',
    help='start with no music')
parser.add_argument('-S', '--straight', action='store', type=str,
    help='go straight to the named game')
parser.add_argument('-M', '--money', default=100, metavar='VALUE',
    help='set money to value')
parser.add_argument('-d', '--debug', action='store_true',
    help='run game in debug mode')
parser.add_argument('-F', '--FPS', action='store_true',
    help='show FPS in title bar')
parser.add_argument('-p', '--profile', action='store_true',
    help='run game with profiling')
parser.add_argument('-B', '--bots', action='store_true',
    help='enable test bots')
parser.add_argument('-N', '--iterations', action='store', type=int,
    help='maximum number of iterations to run for (useful with profiling option')
args = vars(parser.parse_args())

print(args)
Output:
metulburr@ubuntu:~$ python test3.py -m {'profile': False, 'fullscreen': False, 'iterations': None, 'center': True, 'money': 100, 'straight': None, 'FPS': False, 'debug': False, 'bots': False, 'music_off': True, 'winpos': (0, 0), 'size': (800, 600)} metulburr@ubuntu:~$ python test3.py -md {'profile': False, 'fullscreen': False, 'iterations': None, 'center': True, 'money': 100, 'straight': None, 'FPS': False, 'debug': True, 'bots': False, 'music_off': True, 'winpos': (0, 0), 'size': (800, 600)} metulburr@ubuntu:~$ python test3.py -w 100 100 {'profile': False, 'fullscreen': False, 'iterations': None, 'center': True, 'money': 100, 'straight': None, 'FPS': False, 'debug': False, 'bots': False, 'music_off': False, 'winpos': ['100', '100'], 'size': (800, 600)} metulburr@ubuntu:~$ python test3.py -md -w 100 100 {'profile': False, 'fullscreen': False, 'iterations': None, 'center': True, 'money': 100, 'straight': None, 'FPS': False, 'debug': True, 'bots': False, 'music_off': True, 'winpos': ['100', '100'], 'size': (800, 600)} metulburr@ubuntu:~$
As you can see you would have to add a lot of string manipulation to handle what argparse does if you used sys.argv. It also doesnt matter regarding the file. If you compiled your py file into an exe then you would just change the extension and path to the exe with the same args.
Metulburr,

Thanks for the info on using argparse but I don't think that answers my 3 original questions.

Take a look at the OP if you (or others) don't mind.

(Your response did result in me spending some time understanding argparse and your info along with https://www.youtube.com/watch?v=rnatu3xxVQE more or less got me there... I think :-)

thanks
Quote:# example 1 - launch a program and pass it some text as an argument
> myprog.exe arg1

# example 2 - launch a program and pass it a file
> vlc test.wav

# example 3 - launch a program and pass it some arguments and get the results
> set /a 2+3
> 5
If you want to start another process such as vlc and give it an argument with python, then you would use the same procedure via argparse. You would just pass the arguments along to the command to start vlc. subprocess.Popen('vlc {}'.format(ARGUMENT))

To evaluate an expression safely without using eval() you could use sympy
example...
>>> import sympy
>>> sympy.sympify('1+2')
3