how to “source” file into python script

后端 未结 7 1016
孤街浪徒
孤街浪徒 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:11

    You could use execfile:

    execfile("/etc/default/foo")
    

    But please be aware that this will evaluate the contents of the file as is into your program source. It is potential security hazard unless you can fully trust the source.

    It also means that the file needs to be valid python syntax (your given example file is).

提交回复
热议问题