Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to execute a python script file with an argument? [duplicate]

Tags:

python

Possible Duplicate:
Command Line Arguments In Python

Example:

itp.threads[0].memload(r"c:\FistForSOCAD_PASS.bin","0x108000P")
itp.threads[0].cv.cip = "0x108000"
itp.go()
itp.halt()

I would like to make above example as a python script and "FistForSOCAD_PASS.bin" can be replaced by an argument. Because it is only the variable thing.

Example command to run the script will be like:

python itp.py FistForSOCAD_PASS.bin

like image 475
SeiZai Avatar asked Feb 12 '26 23:02

SeiZai


2 Answers

There are a number of ways, One is this approach:

import argparse

parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument('integers', metavar='N', type=int, nargs='+',
                   help='an integer for the accumulator')
parser.add_argument('--sum', dest='accumulate', action='store_const',
                   const=sum, default=max,
                   help='sum the integers (default: find the max)')

args = parser.parse_args()
print args.accumulate(args.integers)

Which gives:

$ prog.py -h
usage: prog.py [-h] [--sum] N [N ...]

Process some integers.

positional arguments:
 N           an integer for the accumulator

optional arguments:
 -h, --help  show this help message and exit
 --sum       sum the integers (default: find the max)

Read more at docs.python.org

like image 77
Fredrik Pihl Avatar answered Feb 15 '26 11:02

Fredrik Pihl


You may use sys.argv:

sys.argv

The list of command line arguments passed to a Python script. argv[0] is the script name (it is operating system dependent whether this is a full pathname or not). [...]

Example:

import sys
if len(sys.argv) > 1:
    filename = sys.argv[1]
like image 22
moooeeeep Avatar answered Feb 15 '26 11:02

moooeeeep



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!