I'm running Python 3.2 on Win XP. I run a python script thru a batch file via this:
C:\Python32\python.exe test.py %1
%1
is an argument that i pass to do some processing in the python script.
I have 2 variables in the batch file that I also want to send as arguments to the python script.
set $1=hey_hi_hello
set $2=hey_hi
I want to be able to do something like this if possible:
C:\Python32\python.exe test.py %1 $1 $2
And then retrieve these argiments in the python script via sys.argv[2]
and sys.argv[3]
Would appreciate any help with this. Thank you.
In Python, arguments are passed to a script from the command line using the sys package. The argv member of sys ( sys. argv ) will store all the information in the command line entry and can be accessed inside the Python script. Python's getopt module can also be used to parse named arguments.
Batch scripts support the concept of command line arguments wherein arguments can be passed to the batch file when invoked. The arguments can be called from the batch files through the variables %1, %2, %3, and so on.
Batch parameters (Command line parameters): In the batch script, you can get the value of any argument using a % followed by its numerical position on the command line. The first item passed is always %1 the second item is always %2 and so on. If you require all arguments, then you can simply use %* in a batch script.
your_script.bat:
set VAR_1=this
set VAR_2=that
python your_script.py %1 %VAR_1% %VAR_2%
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With