Created
August 16, 2015 08:21
-
-
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.
This file contains hidden or 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 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