N
N
Nikotin8882020-07-25 18:00:57
Python
Nikotin888, 2020-07-25 18:00:57

How to open and close a program with python?

How to open and close any program on windows desktop using python?
I took the Telegram Desktop application as an example, below is the code to open it:

import subprocess

subprocess.call('C:/Users/Johnny/AppData/Roaming/Telegram Desktop/t1/Telegram.exe')


Python starts the application. I decided to add that after 5 seconds the application would close:
import subprocess
import time
import psutil

time.sleep(5)
for process in (process for process in psutil.process_iter() if process.name()=="Telegram.exe"):
    process.kill()


The application does not close, but if you start Telegram manually and run the code below:
import psutil

for process in (process for process in psutil.process_iter() if process.name()=="Telegram.exe"):
    process.kill()


Now the application has closed.
Tell me how to open and close a program using python?

Thanks in advance! Here is the code in full:
import subprocess
import time
import psutil

subprocess.call('C:/Users/Johnny/AppData/Roaming/Telegram Desktop/t1/Telegram.exe')
time.sleep(5)
for process in (process for process in psutil.process_iter() if process.name()=="Telegram.exe"):
    process.kill()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
fedosssss, 2020-07-25
@Nikotin888

import os os.startfile(r'C:/Users/Johnny/AppData/Roaming/Telegram Desktop/t1/Telegram.exe')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question