1

**Updated because of comments

I am trying to pass a variable from a batch file to my python files. I am getting a n unexpected result; my code:

In my batch file:

SET variablename = 'name'
python 01_preparing.py %variablename%

In my python file

import sys

print('Argument:', sys.argv[1])

Result is

Traceback (most recent call last):
File "01_preparing.py", line 5, in <module>
print('Argument:', sys.argv[1])
IndexError: list index out of range

Started out with this example: Sending arguments from Batch file to Python script

7
  • 1
    you want another % sign, i.e. python 01_preparing.py %variablename% might work. (I'm typing this from a linux box and my memory is ~10 years old) Commented Oct 16, 2018 at 19:26
  • 4
    1) you use a variable with %variablename%. 2) remove any spaces around = - they become part of the variable name / it's value. Commented Oct 16, 2018 at 19:26
  • it doesnt seem to work; I am probably implementing it wrong, ill update the question Commented Oct 16, 2018 at 19:29
  • ok. So I should use os in stead of sys Commented Oct 16, 2018 at 19:35
  • 2
    @Zuenie, that is what Stephan was pointing out with his 2nd bullet point. But if you would open up a command prompt and type: set /?, you could read the help file, which shows the correct syntax. SET [variable=[string]]. No spaces in that syntax. Commented Oct 16, 2018 at 20:00

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.