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

6

34

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

Eldwin answered 11/4, 2012 at 19:27 Comment(3)
Why would you want this?Flare
I can think of a few reasons ;)Cairngorm
In order to update a script from using a network socket, I need to delete the original script after receiving the updated oneEldwin
C
7

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

# C:\test.py
import os
os.startfile(r"C:\sampleBatch.bat")

In the batch

# C:\sampleBatch.bat
TASKKILL /IM "process name" #For me, this was "ipy64.exe" because I use IronPython.
DEL "C:\test.py"

You may not even need to kill the process to delete the file, but it is safer to do so. Hope this helps.

Cowden answered 11/4, 2012 at 19:30 Comment(4)
And is there a way to self destruct the batch file after closing the script?Eldwin
The batch file should be able to destroy the file upon process closure. Use the DEL "scriptpathhere" command in the batch file to do so. I'll put up a small snippet in a sec.Cowden
The batch file should be able to delete itself, if it's the last thing it does. But you'll get an error »The batch file cannot be found« in that case at the end.Album
to delete the batch file, create another batch file.Theretofore
U
37

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"

Uxmal answered 11/12, 2013 at 23:49 Comment(1)
actually, every module is injected with the __file__ variable, which contains the full-path to the .py file. just os.remove(__file__) no need to check argv, or other importsMechanistic
B
12

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:

import sys, subprocess 
subprocess.Popen("python -c \"import os, time; time.sleep(1); os.remove('{}');\"".format(sys.argv[0]))
sys.exit(0)

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.

Balm answered 10/4, 2015 at 19:49 Comment(1)
In Python 3, you have to add shell=True.Jiggermast
C
7

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

# C:\test.py
import os
os.startfile(r"C:\sampleBatch.bat")

In the batch

# C:\sampleBatch.bat
TASKKILL /IM "process name" #For me, this was "ipy64.exe" because I use IronPython.
DEL "C:\test.py"

You may not even need to kill the process to delete the file, but it is safer to do so. Hope this helps.

Cowden answered 11/4, 2012 at 19:30 Comment(4)
And is there a way to self destruct the batch file after closing the script?Eldwin
The batch file should be able to destroy the file upon process closure. Use the DEL "scriptpathhere" command in the batch file to do so. I'll put up a small snippet in a sec.Cowden
The batch file should be able to delete itself, if it's the last thing it does. But you'll get an error »The batch file cannot be found« in that case at the end.Album
to delete the batch file, create another batch file.Theretofore
G
5

You could also make use of the atexit module.

import os, atexit

atexit.register(lambda file = __file__: os.remove(file))
Geniagenial answered 30/4, 2021 at 13:39 Comment(0)
K
4

Yes, you could use the following:

import os
import sys
import subprocess

# execute and remove after run
(Your python code)
# end of file

dir = os.getcwd()
os.remove(dir+'\%s' % sys.argv[0])

This script can be modified of course, but besides that this should work

Knockknee answered 19/1, 2018 at 21:30 Comment(1)
What is subprocess used for?Immorality
S
2

There is a rather simple method:

import os   

os.remove("insert the file's path")

If you're facing problems, place an 'r' before the starting quotations mark.

Subedit answered 19/4, 2017 at 10:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.