How can I make a python script change itself?
<p>How can I make a python script change itself?</p>

<p>To boil it down, I would like to have a python script (<code>run.py</code>)like this</p>

<pre><code>a = 0
b = 1
print a + b
# do something here such that the first line of this script reads a = 1
</code></pre>

<p>Such that the next time the script is run it would look like</p>

<pre><code>a = 1
b = 1
print a + b
# do something here such that the first line of this script reads a = 2
</code></pre>

<p>Is this in any way possible? The script might use external resources; however, everything should work by just running the one <code>run.py</code>-file.</p>

<p>EDIT:
It may not have been clear enough, but the script should update itself, not any other file. Sure, once you allow for a simple configuration file next to the script, this task is trivial.</p>

<p><strong>Answer:</strong></p>

<p>It is actually much easier than thought. @khelwood 's suggestion works just fine, opening the script and writing it's own content to it is completely unproblematic. @Gerrat's solution also works nicely. This is how I'm having it:</p>

<pre><code># -*- coding: utf-8 -*-
a = 0
b = 1
print a + b

content = []
with open(__file__,"r") as f:
    for line in f:
        content.append(line)

with open(__file__,"w") as f:
    content[1] = "a = {n}\n".format(n=b)
    content[2] = "b = {n}\n".format(n=a+b)
    for i in range(len(content)):
        f.write(content[i])
</code></pre>

<p>What you're asking for would require you to manipulate files at the {sys} level; basically, you'd read the current file in, modify it, over-write it, and reload the current module.  I played with this briefly because I was curious, but I ran into file locking and file permission issues.  Those are <em>probably</em> solvable, but I suspect that this isn't really what you want here.  </p>

<p>First: realize that it's generally a good idea to maintain a separation between code and data.  There are exceptions to this, but for most purposes, you'll want to make the parts of your program that can change at runtime read their configuration from a file, and write changes to that same file.  </p>

<p>Second: idomatically, most python projects use <a href="http://yaml.org/" rel="nofollow">YAML</a> for configuration</p>

<p>Here's a simple script that uses the yaml library to read from a file called 'config.yaml', and increments the value of 'a' each time the program runs: </p>

<pre><code>#!/usr/bin/python
import yaml

config_vals = ""
with open("config.yaml", "r") as cr:
   config_vals = yaml.load(cr)

a = config_vals['a']
b = config_vals['b']
print a + b

config_vals['a'] = a + 1
with open("config.yaml", "w") as cw:
   yaml.dump(config_vals, cw, default_flow_style=True)
</code></pre>

<p>The runtime output looks like this: </p>

<pre><code>$ ./run.py
3 
$ ./run.py
4
$ ./run.py
5 
</code></pre>

<p>The initial YAML configuration file looks like this: </p>

<pre><code>a: 1
b: 2
</code></pre>

python runtime interpreter