开发者

Running scripts whose purpose is to change shell variables with subprocess.popen

I have a series of scripts I am automating the calling of in python with subprocess.Popen. Basically, I call script A, then script B, then script C开发者_运维知识库 and so forth.

Script A sets a bunch of local shell variables, with commands such as set SOME_VARIABLE=SCRIPT_A, set PATH=%SCRIPT_A:/=\;%PATH%.

Script B and C then need to have the effects of this. In unix, you would call script A with "source script_a.sh". The effect lasts in the current command window. However, subprocess.Popen effectively launches a new window (kind of).

Apparently subprocess.Popen is not the command I want to do this. How would I do it?

edit I have tried parsing the file (which is all 'set' statements) and passing them as a dictionary to 'env' in subprocess.Popen, but it doesn't seem to have all worked..


You can use the env argument to Popen with a python dictionary containing the variables then you don't need to run the command that just sets variables.


if 'script A' get generated by another process, you will either need to change the other process so the output file is in a format that you can source (import) into your python script. or write a parser in python that can digest the vars out of 'Script A' setting them within your python script.


If all you want is to call a series of batch files using Python you could create a helper batch file which would call all these batch files like this

call scriptA;
call scriptB;
call scriptC;

and run this helper batch file using subprocess.Popen

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜