0

I have a bat file from where I am calling a python script. I need to pass a variable value from the python script to the bat file. The python script is present in another folder and is not in the same level/scope as that of the bat file. My folder structure is as follows:

Folder_Structure

  • demo/test_val.py
  • build.bat

I am doing the following but it does not work

test_val.py:

import os

name="Redwood"
os.putenv("NAME",name)

build.bat:

@echo off
python demo/test_val.py
echo %NAME%

I am unable to figure out how to achieve this. I don't want to call the bat from my python script instead it should be the other way round i.e. bat should call the python script. Please help, any alternate solutions to get the var name from my python script to my bat file is also welcome. Thanks.

deb
  • 15
  • 7
  • It is answered here. https://stackoverflow.com/questions/2860153/how-do-i-get-the-parent-directory-in-python – manu datta Dec 01 '21 at 14:37
  • Hi @Squashman yes that was a mistake, i have modified my question accordingly. Thanks – deb Dec 01 '21 at 14:38
  • I guess the environment you write the variable `NAME` in Python to is no longer available as soon as the Python script terminates… – aschipfl Dec 01 '21 at 14:40
  • When you open your batch file, it inherits the environment at that time, for that cmd.exe session. Any global changes within that session will not be updated for that session, only local ones will. Your python script is not updating the environment locally for the cmd.exe session, it is only updating the environment locally for the python session, as soon as that python session ends, it will no longer be locally defined, and will not exist in the local cmd.exe session. – Compo Dec 01 '21 at 15:06
  • Thank you for your comments everyone. I will try to replace the batch file with a python script instead, I think that's the better way to do. Thanks – deb Dec 02 '21 at 14:08

0 Answers0