Skip to content

Instantly share code, notes, and snippets.

@goldsmith
Created August 20, 2013 04:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save goldsmith/6277208 to your computer and use it in GitHub Desktop.
Save goldsmith/6277208 to your computer and use it in GitHub Desktop.
Short python program that modifies and calls itself.
import os, subprocess
ITERATIONS = 5
def transform(line):
return "ITERATIONS = " + str(int(line.split()[2]) - 1) + "\n" if line.startswith("ITERATIONS") else line
with open(os.path.abspath(__file__), "r+") as f:
new = ''.join(map(transform, f.readlines()))
f.seek(0)
f.write(new)
if ITERATIONS > 1:
print "iterations left: %s" % ITERATIONS
print subprocess.Popen(["python", os.path.abspath(__file__)], stdout=subprocess.PIPE).communicate()[0],
else:
print "final iteration!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment