Search code examples
pythonpython-3.xbatch-filefile-handlingpopen

How to kill a batch file with python?


How could I stop a running file or kill it (.bat file) ?

I used a code in function to open it :

p = subprocess.Popen(str(filepath), shell=True, stdout = subprocess.PIPE)

so I want to make another function that kills/close the same file but I need help with that

I tried something like that :|

p = subprocess.Popen('exit ' + str(filepath), shell=True, stdout = subprocess.PIPE)

I need your help please and Thanks ♥


Solution

  • In terminal to close a opened programe you can use: taskkill /IM AppName >nul Example: taskkill /IM notepad++.exe >nul

    Now you can use os library to type that:

    #// Import
    import os
    
    #// Global Var's
    appName:str = "notepad++"
    appExtension:str = "exe"
    
    #// Logic
    os.system(f'cmd /c "taskkill /IM {appName}.{appExtension} >nul"')