【问题标题】:Config.ini Cannot be read, or foundConfig.ini 无法读取或找不到
【发布时间】:2020-08-07 20:42:07
【问题描述】:

所以我的 config.ini 在我的下载中,我的 python 文件在我的

D:/Python Code Stuff I did

而config.ini被放置在

C:\Users\karam\Downloads\Vector_1.3\Vector_1.3\settings.ini

当我尝试这段代码时

config = ConfigParser()
path = os.path.dirname(os.path.abspath(__file__))
configPath = os.path.join(path, "settings.ini")
print(config.sections())
config.read('settings.ini')

我收到的错误是

C:\Users\karam>D:/y/python.exe "d:/Python Code stuff I did/print.py"
[]

它只打印括号,当它应该打印部分时,例如

【问题讨论】:

  • 错误在哪里?
  • C:\Users\karam>D:/y/python.exe "d:/Python Code stuff I did/print.py" []

标签: python file config ini


【解决方案1】:

您在读取文件之前调用了config.sections()。您需要先读取文件。

您正在查找文件路径而不是使用它。你应该在config.read() 方法中使用configPath

试试这个:

config.read(configPath)
print(config.sections())

【讨论】:

  • 仍然做同样的事情,它只打印括号
猜你喜欢
  • 2010-11-04
  • 2021-03-12
  • 2012-10-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-26
  • 2011-11-30
  • 2020-11-13
相关资源
最近更新 更多