使用ConfigParse编辑具有无名部分的文件

时间:2018-11-13 17:09:32

标签: python configparser

我正在尝试编辑一个Splunk配置文件,该文件的节没有名字

Conf文件

# Application-level permissions

[]
access = read : [ * ], write : [ admin, power ]

代码

>>> import configparser
>>> config = configparser.RawConfigParser()
>>> config.read('sh-apps-pro/indexed_volume/metadata/default.meta')
Traceback (most recent call last):
  File "<input>", line 1, in <module>
    config.read('sh-apps/pro/indexed_volume/metadata/default.meta')
  File "/usr/local/Cellar/python@2/2.7.15_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/backports/configparser/
__init__.py", line 705, in read
    self._read(fp, filename)
  File "/usr/local/Cellar/python@2/2.7.15_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/backports/configparser/
__init__.py", line 1101, in _read
    raise MissingSectionHeaderError(fpname, lineno, line)
MissingSectionHeaderError: File contains no section headers.
file: 'sh-apps-pro/indexed_volume/metadata/default.meta', line: 4
u'[]\n'

我尝试使用以下方式将default_section设置为空字符串

config = configparser.RawConfigParser(default_section='')

但是看起来好像不是有效名称,因为它会引发相同的异常

0 个答案:

没有答案
相关问题