Python - writing to python files?
<p>Is there some more convenient way to write into python files than using read/write for any file (like txt files etc).</p>

<p>I mean python knows what actually is the structure of python file, so if I need to write into it, maybe there is some more convenient way to do it?</p>

<p><strong>If there is no such way</strong> (or it is too complicated), then what would best way to normally modify python file just using normal <code>write</code> (example below)?</p>

<p>I have lots of these files in my subdirectories called:</p>

<p><code>__config__.py</code></p>

<p>These files are used as configuration. And they have unassigned python dictionary, like this:</p>

<pre><code>{
  'name': 'Hello',
  'version': '0.4.1'
}
</code></pre>

<p>So what I need to do, is write to all those <code>__config__.py</code> files new version (for example <code>'version': '1.0.0'</code>).</p>

<p><strong>Update</strong></p>

<p>To be more specific, given that there is a python file with a content like this:</p>

<pre><code># Some important comment
# Some other important comment
{
'name': 'Hello',
'version': '0.4.1'
}
# Some yet another important comment
</code></pre>

<p>Now running some python script, it should write into python file modifying given dictionary and after writing, output should be like this:</p>

<pre><code># Some important comment
# Some other important comment
{
'name': 'Hello',
'version': '1.0.0'
}
# Some yet another important comment
</code></pre>

<p>So in other words, write should only modify <code>version</code> key value, and everything else should be kept like it was before writing.</p>