how to “source” file into python script

后端 未结 7 1017
孤街浪徒
孤街浪徒 2020-12-10 11:39

I have a text file /etc/default/foo which contains one line:

FOO=\"/path/to/foo\"

In my python script, I need to reference the

7条回答
  •  情书的邮戳
    2020-12-10 12:01

    Just to give a different approach, note that if your original file is setup as

    export FOO=/path/to/foo
    

    You can do source /etc/default/foo; python myprogram.py (or . /etc/default/foo; python myprogram.py) and within myprogram.py all the values that were exported in the sourced' file are visible in os.environ, e.g

    import os
    os.environ["FOO"] 
    

提交回复
热议问题