Search code examples
pythonoperating-systemtaskkill

How can one prevent a program from being opened with Python?


I want to try to prevent a program from being run with Python. For example, notepad.exe. My idea is the following, but will this work?

import os

i = 0

while i < 1:

    os.system('taskkill /f /im notepad.exe') 

Solution

  • Your solution will work, but it will spawn a lot of console windows one after another. To avoid it you can try this:

    >>> import subprocess
    >>> from time import sleep
    >>> si = subprocess.STARTUPINFO()
    >>> si.dwFlags |= subprocess.STARTF_USESHOWWINDOW
    >>> while True:
            subprocess.call('taskkill /F /IM notepad.exe', startupinfo=si)
            sleep(1) # delay 1 seconds