Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make scripts auto-delete at the end of execution?

Tags:

Is it possible to make a python script that will delete the .py file at the end of its execution (self-delete) in windows?

like image 394
Serban Razvan Avatar asked Apr 11 '12 19:04

Serban Razvan


People also ask

Can a script delete itself?

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.

Can a python file delete itself?

Python, 18Opens itsself in write-only mode, erasing itself. This truncates the file. It does not delete it.

How do you stop a script execution in Python?

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.


1 Answers

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"

like image 187
Neomind Avatar answered Oct 28 '22 10:10

Neomind