This is short, and hopefully not completely uninteresting etc.
I just wanted to be able to do this, and thought it would be pretty neat.
The self-destruction of the file is achieved by it securely deleting itself.
#!/usr/bin/env python import subprocess as sp from os import path # This gives us the absolute(full) path to this python script self_path = path.abspath(__file__) # Do stuff -- I just created a folder sp.call(["mkdir", "/home/User/Desktop/thinair"]) # At the end of the script, the file shreds itself sp.call(["/usr/bin/shred", "-fuz" , self_path])
Basically you get the scripts own file path using _file_(stackoverflow), and you can then delete it in whatever way you like when the script has finished.
I used the Linux
shred cmd, where:
-f, --force change permissions to allow writing if necessary -u truncate and remove file after overwriting -z, --zero add a final overwrite with zeros to hide shredding
**On Windows:** ```python import os # This gives us the absolute(full) path to this python script file_path = os.path.abspath(__file__) # Do stuff -- I just created a folder os.system("mkdir %USERPROFILE%\Desktop\dontlook") # At the end of the script, the file is deleted & over-written os.remove(file_path) folder_path = os.path.dirname(file_path) os.system("cipher /W:%s" % folder_path) ``` The Python codes needed to actually delete a file/folder are given in [this](https://stackoverflow.com/questions/6996603/how-do-i-delete-a-file-or-folder-in-python) stackoverflow accepted answer, those being: <code>os.remove()</code> will remove a file. <code>os.rmdir()</code> will remove an empty directory. <code>shutil.rmtree()</code> will delete a directory and all its contents. As you may have guessed, this doesn't overwrite the deleted data like <code>shred</code> does on Linux. However, there is a Windows command <code>cipher</code> whose particulars you can see for yourself either by looking [here](https://support.microsoft.com/en-us/kb/315672) or typing <code>cipher /?</code> into a CMD prompt. The following windows command will overwrite _deleted_ data on the specified volume and/or directory: <code>cipher /w:drive_letter:\folder_name</code> My own tests on Windows 7 successfully delete the script and _appear_ to overwrite the data... however, since I haven't attempted to retrieve the deleted script forensically, I can't yet vouch for that 100%. But the Windows docs for <code>cipher</code> above do seem confident with themselves. (_Something to maybe bear in mind.. possible very lengthy overwrite times with_ <code>cipher</code> _depending on the size of the target folder/drive. Test this out first._) ~~~~~~~~~ So, no great scripting revelations here :grin:, I'm sure most of you would know how to do this if you wanted to, but like I said, I just liked the idea of a script deleting itself after performing it's duties, maybe in an attempt to cover it's own tracks. Perhaps it could come in handy in certain situations? Thanks for reading ~~~~~~~~