如何“来源"文件到python脚本中 [英] how to "source" file into python script
本文介绍了如何“来源"文件到python脚本中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个文本文件/etc/default/foo
,其中包含一行:
I have a text file /etc/default/foo
which contains one line:
FOO="/path/to/foo"
在我的python脚本中,我需要引用变量FOO.
In my python script, I need to reference the variable FOO.
将文件/etc/default/foo
源"到我的python脚本中的最简单方法是什么,就像我在bash中所做的一样?
What is the simplest way to "source" the file /etc/default/foo
into my python script, same as I would do in bash?
. /etc/default/foo
推荐答案
答案与@jil相同,但该答案特定于某些历史版本的Python.
Same answer as @jil however, that answer is specific to some historical version of Python.
在现代Python(3.x)中:
In modern Python (3.x):
exec(open('filename').read())
从2.x替换execfile('filename')
这篇关于如何“来源"文件到python脚本中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文