Skip to content

Instantly share code, notes, and snippets.

@jonmoshier
Created April 30, 2018 18:54
Show Gist options
  • Save jonmoshier/3eee6ce29f36cb3a0e422d94bbb35a73 to your computer and use it in GitHub Desktop.
Save jonmoshier/3eee6ce29f36cb3a0e422d94bbb35a73 to your computer and use it in GitHub Desktop.
Cleaning out my hard drive and I found this self replicating python file I wrote 5 years ago. Saving for posterity.
#!/usr/bin/env python
# use unittest to test if new file equals original, will
# fail test if it does not.
import unittest
# Get the file name of 'this' file. Will be 'file.py'
ORIG_FILE_NAME = __file__
# Get the contents of the ORIGINAL FILE file.
original_files_contents = open(ORIG_FILE_NAME,'rb').read()
# Create new file name by splitting original name 'file' and 'py'
# and putting '_new' between them. New file is 'file_new.py'.
NEW_FILE_NAME = ("%s_%s.%s" % (ORIG_FILE_NAME.split('.')[0], "new",
ORIG_FILE_NAME.split('.')[1] ) )
# Write the contents of the original file to the new file.
with open(NEW_FILE_NAME, 'wb') as file:
file.write(original_files_contents)
# Test that new file has same contents as original
class TestClone(unittest.TestCase):
def test_diff_contents(self):
f1 = open(ORIG_FILE_NAME, 'rb').read()
f2 = open(NEW_FILE_NAME, 'rb').read()
self.assertEqual(f1, f2)
if __name__ == "__main__":
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment