Dangerous Self Deleting Python Script
Updated: Apr 29
Today, I came across one of the most interesting little Python scripts that is so simple yet so important to memorize it will blow your mind. Blow it clean off.
I ran across this code on a site called Geeks for Geeks. There is a gold mine of FREE Python code that is actually useful that you should be practicing on a regular basis. I highly recommend that site for beginners to Python or programming and coding in general. Python is the easiest way to start, it has a great name, can be used to program Raspberry Pi boards and make Discord chat bots, etc. The list goes on and on. If you haven't already, get the latest version of Python and the Interactive shell here.
Here is a snapshot of the code taken from the website. If you intend to copy and paste it, you are going to need to visit the site it was originally posted on for that.
This will COMPLETELY delete your file.
It will NOT be in your recycle bin.
It will vanish into thin air.
Go ahead, look for it after....you won't find it.
Use at your own risk.
You need to import the os module first. Next you just allow the script to assign a variable to the file that you are intending to delete. After that, you need to write out the path and assign a variable name to that path. Keep in mind, you will get an error, at least on a Windows PC if the path has ' \ ' in the path. Easy to fix! Just change them to ' / ' . The third step is to combine the path to the file name into a third variable that they have labeled as 'path' here. You can name is whatever you want.
The last step is to use the os.remove() command with the name of that third variable you mashed the other 2 into in the ( ). The print function is not necessary, just for show here.
Imagine what you could do with this simple to learn, memorize and use code!
Just replace the 'file.txt' with whatever you intend to delete and copy and paste the path to that file in the second variable they chose to label as 'location'.
Imagine using this to erase data with the push of a button. You could push a button on an Arduino, AVR, PIC, ARM, Raspberry Pi or ANY kind of chip and program it to send out a pulse via serial output (serial) to your PC which could delete the file specified.
You could even program a menu for a potentiometer and LCD for your chip that you use for the push button, have it connected to your PC via bluetooth and delete the file you chose in your menu from across the room.
That just scratches the surface.
How could you create code in Python that deletes more than just files?
I hope this was easy for you to understand.
Sometimes, the smallest, simplest code can become the most vital asset to accomplishing a task.
Now, go and make your version of this code. DON'T delete any real files. Make and save a junk text file to your PC and try this code out to delete it. Maybe add some other code to run after the file is deleted.
Let me show you how to understand, code and program electronics and robots in a simpler, easier way using less components, saving more money and still making those amazing dreams you have come to life.