Skip to content

Instantly share code, notes, and snippets.

@the-c0d3r
Created August 16, 2015 08:21
Show Gist options
  • Save the-c0d3r/7a211dc469d9413672b9 to your computer and use it in GitHub Desktop.
Save the-c0d3r/7a211dc469d9413672b9 to your computer and use it in GitHub Desktop.
This is a simple way to demonstrate how the python file can rewrite it's own source code. This is particularly useful for checking if the program is first run or not. Something like that.
import sys
con = '#'
if __name__ == '__main__':
fname = sys.argv[0]
content = open(fname).read()
newfile = open(fname,'w')
if chr(35) in content:
content = content.replace(chr(35),chr(47))
print"Changed %s to %s" % (chr(35),chr(47))
elif chr(47) in content:
content = content.replace(chr(47),chr(35))
print "Changed %s to %s" % (chr(47),chr(35))
newfile.write(content)
newfile.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment