The following code is supposed to stop the batch file counting down to ZERO when the KeyboardInterrupt error is called. But it is not doing so. with open('test.bat', 'w', encoding='utf-8') as file: file.write('@echo off\nTIMEOUT /t 20') with open('test.txt', 'w', encoding='utf-8') as file: file.write('test file') p1 = None SW_HIDE = 1 info = subprocess.STARTUPINFO() info.dwFlags = subprocess.STARTF_USESHOWWINDOW info.wShowWindow = SW_HIDE p1 = subprocess.Popen('test.bat', startupinfo=info, creationflags=CREATE_NEW_CONSOLE) print(f"Subprocess started with PID: {p1.pid}") try: while p1.poll() is None: time.sleep(0.5) except KeyboardInterrupt as e: print('program ended') p1.terminate(); p1.wait() os.remove('test.txt') quit() print('continue code') os.remove('test.txt') Continue reading...