0

I am having trouble getting the second argument I am sending to a Python script? I am unsure of how to fix it and changing the second argument to a value or putting a %1 in front of the arguments has not worked. Here is my current code does anyone know what the error might be?

Batch File Code:

`@echo off`
set /p UserInputPath=Enter the location of your development unit?
set /p ScriptPath =Enter script path?
python part2.py %UserInputPath% %ScriptPath%

Python File Code:

unitPath = sys.argv[1]
scriptLocation = sys.argv[2]

scriptLocation = sys.argv[2] IndexError: list index out of range

John S
  • 21
  • 4
  • 2
    I've never done any Batch programming, but the empty space between `ScriptPath` and `=` could cause the issue.. Otherwise, try in your python script, above `scriptLocation = sys.argv[2]` to `print(sys.argv)` it should be a list of the arguments passed. If you don't get your supplied arguments, the problem is within the batch code – Patrik Feb 15 '18 at 13:40
  • debug this by doing `print sys.argv` (for python2) or `print(sys.argv)` for python3 so you see the content of `sys.argv` – Edwin van Mierlo Feb 15 '18 at 13:44
  • @Sativa: Bang on! the posted code sets a variable named `scriptpath[space]`, not `scriptpath`, hence the Python routine sees only one argument, not two. I'd suggest you post your comment as an answer. – Magoo Feb 15 '18 at 13:45
  • The extra space was this issue. Thanks for your help! – John S Feb 15 '18 at 13:48
  • Yes, If you read the help file for the `SET` command you would see the syntax plain as day. `SET [variable=[string]]`. No spaces. – Squashman Feb 15 '18 at 15:31

1 Answers1

0

The Problem is within your Batch File, as you can see in this answer, using spaces within the declaration of Variablenames, will result in the space being a part of it.

Patrik
  • 440
  • 3
  • 16