How to Close a Program Using Python

How to Close a program using python?

# I have used os comands for a while
# this program will try to close a firefox window every ten secounds

import os
import time

# creating a forever loop
while 1 :
os.system("TASKKILL /F /IM firefox.exe")
time.sleep(10)

How to terminate external program or window using python

your open_program function is recursive ???

Anyway, to open your program, you can do it by the path name. It then returns a handle on a subprocess.
To close it, just call the terminate method on that handle

example below opens notepad, waits 3 seconds, then closes it.

import time
import subprocess

def open_program(path_name):

return subprocess.Popen(path_name)

def close_program(p):
p.terminate()

p=open_program("notepad")
time.sleep(3)
close_program(p)

Closing a program using python?

I think I got it working. Instead of using os.kill() I am using

os.system("taskkill /pid " +str(pid)+ " /t")

This closes the recording without corrupting the file. Also if anyone else has this problem and is recording with VLC command line, this does not seem to work if the dummy interface is used.

How can I close a Python program properly when in an IDE?

Nothing past root.mainloop() will execute because it never returns. Move the with keyboard... code before root.mainloop().

how do I get my program to close once finished in python [closed]

import sys

...

if number == secret_number:
print("well done")
sys.exit() # Exit from Python

You don't want your IDLE to exit right after you guess the number, because you won't have enough time to read the 'well done' prompt.
What could you do? Well, one idea is to just pause the program briefly before exiting:

import sys
import time
...
if number == secret_number:
print("well done")
time.sleep(5) #Pause for 5 seconds
sys.exit()


Related Topics



Leave a reply



Submit