Is it possible to make a python script that will delete the .py file at the end of its execution (self-delete) in windows?
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
This way makes your program non OS dependant.
Bonus points: When parsing arguments the very first argument that you get in sys.argv is equals to "path-to-filename/filename.py"
Yes, you could use the following:
This script can be modified of course, but besides that this should work
There is a rather simple method:
If you're facing problems, place an 'r' before the starting quotations mark.
I'm not sure deleting a file while it's in memory would be a good idea. Try running a batch file from the script which closes the script process, then deletes the script file.
There may be a native method to self destruct a script, but I am not aware of it.
EDIT: Here is a simple example of how you could accomplish this using the method I described:
In the script
In the batch
You may not even need to kill the process to delete the file, but it is safer to do so. Hope this helps.
Neomind's answer seems to do the trick. But if deleting the file while it's in memory bothers you, and you're looking for a pure python solution, then you could use subprocess to create a new process with the explicit purpose of deleting your original script file. Something like this should work:
You probably wouldn't need the timeout in there but I've added it just to make sure that the process from the original script has been given enough time to close itself.