1

I have a bat file which sets values to variables and I need to use those variables in python script.

env.bat:

SET USERNAME=username
SET PASSWORD=password

I need the above variables to be used in python script. I've tried the below but I get None as output.

login.py

import os
import subprocess
subprocess.call(['env.bat'])
print(os.getenv('USERNAME'))

Could you please help where I'm wrong or any other way I could achieve this ?

Thanks in advance.

Krishy
  • 23
  • 1
  • 5
  • 2
    Do you only need to read the username and password or save them later as well? You might find an answer here: https://stackoverflow.com/questions/40914880/reading-text-from-a-bat-file-in-to-python – GittingGud Mar 26 '19 at 13:55
  • 1
    firstly, never have spaces between variables, values and the `=` it should be `set "USERNAME=username"` then to send it, you need to get the python script to first read input arguments, but then simply send it from the batch file to pythin script as `python script.py %USERNAME% %PASSWORD%` – Gerhard Mar 26 '19 at 13:57
  • Possible duplicate of [How to read windows environment variable value in python?](https://stackoverflow.com/questions/10496748/how-to-read-windows-environment-variable-value-in-python) – Jeff Zeitlin Mar 26 '19 at 13:59
  • @GittingGud, thank you for the link. I had the same thing in mind but it's more like searching for a string and finding it's value followed by **=**. Is there a way similar to calling the bat file which sets variables and use those variables like `%USERNAME%` in batch script ? – Krishy Mar 26 '19 at 14:09
  • @JeffZeitlin, I have had a look at it and tried the answer. The output is _None_ . I'm posting my python script in question so that you may help correcting it. – Krishy Mar 26 '19 at 14:19
  • @Krishy if your .bat file does always have the same structure you can just read each line into a string and then split the string after the "=": yourstring.split("=") – GittingGud Mar 26 '19 at 14:37
  • @GittingGud It works perfectly fine. I'm also interested to know if there is a way I can call the bat file and use variables directly as in batch scripting. – Krishy Mar 26 '19 at 14:45

2 Answers2

1
import os
import subprocess
p = subprocess.Popen(['cmd', '/v:on', '/q', '/c', 'env.bat', '&echo(!USERNAME!&echo(!PASSWORD!'], stdout=subprocess.PIPE, universal_newlines=True)
stdout, stderr = p.communicate()
username, password = stdout.splitlines()
print(username)
print(password)

Since env.bat does not use setlocal, the environmental variables will be available in the same process of cmd, after env.bat is run. This allows echo the values of !USERNAME! and !PASSWORD! to the stdout stream.

Options of cmd:

  • /v:on enables delayed expansion.
  • /q sets echo off.
  • /c runs the following command string.

Note:

  • Tested in Python 2 and Python 3. Python 2 is missing support for a context manager with subprocess so a with statement was not used.

  • The USERNAME environmental variable is a builtin name. Changing it without good reason may create unexpected issues.

  • As delayed expansion is enabled, use of ! in env.bat may need to be escaped, else ! may be omitted when cmd parses the file. call echo in the command line could avoid the enabling of delayed expansion if needed.

michael_heath
  • 5,262
  • 2
  • 12
  • 22
0

Using os.getenv() you are trying to get an environment variable. So, you need to set an environment variable in your batchfile. This can be done, using the SETX command, as described in this URL.

SETX USERNAME=username
SETX PASSWORD=password
Dominique
  • 16,450
  • 15
  • 56
  • 112
  • I've tried above solution, unfortunately I still get _None_ as my output. And one thing is that I don't want to change my code in bat file which is used by other batch scripts. – Krishy Mar 26 '19 at 17:24