【发布时间】:2016-09-12 22:47:17
【问题描述】:
我正在尝试使用 Python 的 ConfigParser 库从 ini 文件中删除 [section]。
>>> import os
>>> import ConfigParser
>>> os.system("cat a.ini")
[a]
b = c
0
>>> p = ConfigParser.SafeConfigParser()
>>> s = open('a.ini', 'r+')
>>> p.readfp(s)
>>> p.sections()
['a']
>>> p.remove_section('a')
True
>>> p.sections()
[]
>>> p.write(s)
>>> s.close()
>>> os.system("cat a.ini")
[a]
b = c
0
>>>
remove_section() 似乎只发生在内存中,当被要求将结果写回 ini 文件时,没有什么可写的。
关于如何从 ini 文件中删除部分并将其保留的任何想法?
我用来打开文件的模式不正确吗? 我尝试使用 'r+' & 'a+' 并没有奏效。我无法截断整个文件,因为它可能包含不应删除的其他部分。
【问题讨论】:
标签: python ini configparser