How to remove a section from an ini file using Python ConfigParser?
<p>I am attempting to remove a [section] from an ini file using Python's ConfigParser library.</p>

<pre><code>&gt;&gt;&gt; import os
&gt;&gt;&gt; import ConfigParser
&gt;&gt;&gt; os.system("cat a.ini")
[a]
b = c

0

&gt;&gt;&gt; p = ConfigParser.SafeConfigParser()
&gt;&gt;&gt; s = open('a.ini', 'r+')
&gt;&gt;&gt; p.readfp(s)
&gt;&gt;&gt; p.sections()
['a']
&gt;&gt;&gt; p.remove_section('a')
True
&gt;&gt;&gt; p.sections()
[]
&gt;&gt;&gt; p.write(s)
&gt;&gt;&gt; s.close()
&gt;&gt;&gt; os.system("cat a.ini")
[a]
b = c

0
&gt;&gt;&gt;
</code></pre>

<p>It appears that the remove_section() happens only in-memory and when asked to write back the results to the ini file, there is nothing to write.</p>

<p>Any ideas on how to remove a section from the ini file and persist it?</p>

<p>Is the mode that I'm using to open the file incorrect?
I tried with 'r+' &amp; 'a+' and it didn't work. I cannot truncate the entire file since it may have other sections that shouldn't be deleted.</p>