-2

When I pass a variable to a BAT file using the following:

from subprocess import call
# Prompt for UID
UID = raw_input('Enter UID: ')

# Prompt for password
PSWD= raw_input('Enter your password: ')

dir = r"f:\_Python"
cmdline = "setENVvariables.bat UID, PSWD"
rc=call("start cmd /K " + cmdline, cwd=dir, shell=True)

.. the values are not passed. When I echo the input in the BAT file, I get the Python variable name

BAT file

echo %1
echo %2

BAT File output

f:\_Python>echo UID
UID

f:\_Python>echo PSWD
PSWD

f:\_Python>
gaganso
  • 2,914
  • 2
  • 26
  • 43
  • 1
    The `PSWD` in `"setENVvariables.bat UID, PSWD"` is just the four characters "PSWD", not the variable that the user actually typed in. I am voting to close as a typo because you append strings correctly on the line immediately below. – Mad Physicist Jul 30 '18 at 20:26
  • Also, where do you expect UID to come from? – Mad Physicist Jul 30 '18 at 20:27

1 Answers1

0

You're invoking a shell to run start that will run a CMD to run your BAT file!

That means you want to run a subprocess, but end up starting FOUR instead!

  • Don't use shell=True
  • Don't use start unless you really need it
  • Don't execute CMD

By doing this you can just pass your parameters as a list and it will work:

dir = r"f:\_Python"
cmdline = ["setENVvariables.bat", UID, PSWD]
rc = call(cmdline, cwd=dir)
nosklo
  • 217,122
  • 57
  • 293
  • 297
  • given the script name (setENVvariables.bat) I doubt that this is very efficient, since if the script sets some variables, they're just lost after the script exits. – Jean-François Fabre Jul 30 '18 at 20:33
  • @Jean-FrançoisFabre who knows, maybe it sets some env variables before calling some program... – nosklo Jul 30 '18 at 20:34