Is it possible to make a python script that will delete the .py file at the end of its execution (self-delete) in windows?
Only when the last program closes its handle does the file really get deleted. So, in a sense, yes, a shell script can delete itself, but it won't really be deleted until after the execution of that script finishes.
Python, 18Opens itsself in write-only mode, erasing itself. This truncates the file. It does not delete it.
To stop code execution in Python you first need to import the sys object. After this you can then call the exit() method to stop the program from running. It is the most reliable, cross-platform way of stopping code execution.
This way makes your program non OS dependant.
from os import remove from sys import argv remove(argv[0])
Bonus points: When parsing arguments the very first argument that you get in sys.argv is equals to "path-to-filename/filename.py"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With