/etc/sf.cfg
此文件里面全是定义的变量,有很多,大概几百行例如:HAHA='123',HEHE='456'
用python写个脚本需要用到里面的变量,
os.system('source /etc/sf.cfg ') print os.environ.get("HAHA")
但是并获取不到,提示None
请问有其他方法吗?
数据分析师2017-10-01 00:55:02
How can python import all the variables in the text and extract the variable values? -PHP Chinese website Q&A-Python How to import all the variables in the text and extract the variable values? -PHP Chinese website Q&A
Let’s take a look and learn.
迷茫2017-03-20 09:28:56
楼主方法不对.os.system是创建子进程的.所以结果是不会返回到父进程的.
设置环境变量可以如下:
fp = open("/etc/sf.cfg") lines = fp.readlines() fp.close() for line in lines: items = line.split("=") key = items[0].strip() value = items[1].strip() os.environ[key] = value