Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending arguments from Batch file to Python script

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.

like image 356
dawnoflife Avatar asked May 30 '12 19:05

dawnoflife


People also ask

How do you pass arguments to a Python script?

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.

Can a batch file take 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.

How do I pass a command line argument to a batch file?

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.


1 Answers

your_script.bat:

set VAR_1=this
set VAR_2=that

python your_script.py %1 %VAR_1% %VAR_2%
like image 150
Tim Henigan Avatar answered Sep 22 '22 22:09

Tim Henigan