2

Python seems to process my attempt :

subprocess.call(['set' , 'logfile=cat'], shell=True)

It returns no errors. However when I try using logfile as a variable or do %logfile%, it doesn't seem to have set logfile as anything. How does one make batch variables from within a python script?

What I am attempting to do with this is: I have a batch script that sequentially runs several python scripts. I wanted to set a variable from within one of my python scripts that would persist throughout my batch script.

1 Answer 1

1

Your variable is set, but as soon as the call returns, that instance of the shell ends and the variable goes away.

What are you trying to accomplish exactly? This sounds like an XY problem.

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

6 Comments

Oh I see. I have a batch script that sequentially runs several python scripts. I wanted to set a variable from within one of my python scripts that would persist throughout my batch script.
@user3447675 you might be able to do that using an environment variable but I'm far from sure.
Environment variables won't help here. Child processes inherit their parent's environment, but changes to the environment made in a child process aren't reflected in the parent's environment.
@Alp I don't actually know a great way for a python script launched by a batch file to affect the rest of the batch file. I'm turning my answer to community wiki -- if you have a good solution, please edit and post it!
@adsmith It would be a security hole if a child process could unilaterally alter the execution of the parent. You have to do something like this cooperatively: The child can communicate the desired change back to the parent, and the parent can then make the change if it deems it appropriate to do so.
|

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.