简体   繁体   中英

How to delete a .exe file written in python from within the .exe file?

I am trying to create a script which clones a github repository into the current directory and then deletes the script which called it.

The script is written in python 3.7.4 and is compiled into an exe. I've tried using 'os.remove(sys.argv[0])' which works before compiling but will not work for my end application. I have also tried several other deletion methods however none of them worked at all, either with or without compiling into an exe.

import os

def function:
    # code

def main():
    function()
    os.remove(sys.argv[0])

Im looking to have the exe delete itself after running like the.py file does however I dont know the actual method to go around doing this.

you can write a batch file to delete the python file after execution but am not sure if that is a good idea. It is definitely possible though.

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

Th batch file

TASKKILL /IM "process name" 
DEL "C:\test.py"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM