Created
August 20, 2013 04:49
-
-
Save goldsmith/6277208 to your computer and use it in GitHub Desktop.
Short python program that modifies and calls itself.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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