14

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.

dawnoflife
  • 1,572
  • 10
  • 42
  • 62

2 Answers2

18

your_script.bat:

set VAR_1=this
set VAR_2=that

python your_script.py %1 %VAR_1% %VAR_2%
Tim Henigan
  • 60,452
  • 11
  • 85
  • 78
  • 2
    @Tim Henigan: I think you meant `python test.py %1 %VAR_1% %VAR_2%` – Hugh Bothwell May 30 '12 at 19:49
  • 1
    Note that this applies to any sort of environment variable, local or global. So you could do something like `python %USERPROFILE%\scripts\test.py %1 %VAR_1% %VAR_2%`. – JAB May 30 '12 at 20:00
0

Another option is to write arguments right after the python script, following the example:

python your_script.py this that

If you are using Linux .sh file, remember to run dos2unix XXX.sh before you run: bash XXX.sh.

The reason, in a simple version, is that dos and unix use different newline breakers.

Jia Gao
  • 1,172
  • 3
  • 13
  • 26