execfile()用于配置 - 错误没有这样的文件或目录

时间:2015-11-14 08:34:10

标签: python config execfile

我正在尝试使用execfile()函数将文件用作配置文件。该文件只有一堆变量和值。但是,当我尝试在我的代码中使用它时,它会抛出IOError: [Errno 2] No such file or directory

这是我的代码:

dictionary = {}
execfile("myConfig.cfg", dictionary)

1 个答案:

答案 0 :(得分:0)

  1. 我会将myConfig.cfg重命名为myConfig.py
  2. 确保您的配置文件和尝试获取数据的文件位于同一文件夹中。
  3. myConfig.py
  4. dictionary={'data1':'value1','data2':'value2'}

    4.导入数据:

    from myConfig import dictionary print dictionary