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.

7
  • 2
    Do you only need to read the username and password or save them later as well? You might find an answer here: stackoverflow.com/questions/40914880/… Commented Mar 26, 2019 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% Commented Mar 26, 2019 at 13:57
  • Possible duplicate of How to read windows environment variable value in python? Commented Mar 26, 2019 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 ? Commented Mar 26, 2019 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. Commented Mar 26, 2019 at 14:19

2 Answers 2

2
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.

Sign up to request clarification or add additional context in comments.

Comments

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

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.