Python code to close all open application in windows - python

I am working on pyautogui for locateonscreen but I need to make sure I have a clean windows prior to running it.
I need help with a code that will kill/close all open/active windows application and then open a single exe using subprocess.

you can use the psutil library to close all open/active windows applications and then open a single exe using the subprocess module:
import psutil
import subprocess
# Kill all open/active processes
for proc in psutil.process_iter():
proc.kill()
# Open a single exe using subprocess
subprocess.Popen("C:\\path\\to\\exe.exe")
Note that the psutil.process_iter() returns a list of all running processes on the system, and proc.kill() method is used to kill each process. The subprocess.Popen method is used to open the exe file. Make sure that you provide the correct path to the exe file. Please be aware that this script will close all open applications, including any unsaved work in progress.
You should use it with caution, or for testing purpose only.
you can instead reduce all open windows. this is safer!
import win32gui
import win32con
import subprocess
def minimize_all():
def callback(hwnd, hwnds):
if win32gui.IsWindowVisible(hwnd) and win32gui.IsWindowEnabled(hwnd):
hwnds.append(hwnd)
return True
hwnds = []
win32gui.EnumWindows(callback, hwnds)
for h in hwnds:
win32gui.ShowWindow(h, win32con.SW_MINIMIZE)
minimize_all()
# Open a single exe using subprocess
subprocess.Popen("C:\\path\\to\\exe.exe")
This script uses the win32gui library to enumerate all open windows, and the win32con library to minimize them.

why not minimize all windows with shortcut WIN + D ?
import pyautogui
pyautogui.hotkey('winleft','d')

Related

Python's Popen return wrong pid on Windows

I need to open a windows file explorer window in a python script and then close it again.
Apparently Popen provides the wrong pid. So I can't close the explorer window.
Example:
from subprocess import Popen
import os
import signal, time
process = Popen('explorer')
print(process.pid)
time.sleep(2)
# problem: process.pid is wrong
os.kill(process.pid, signal.SIGTERM)
Other programs (e.g. notepad.exe instead of explorer.exe) work fine.
Python 3.10.4
Windows professional 10.0.19044.1645
"Launch folder windows in a separate process" is active
Popen delivers a different pid each time, but always the wrong one.
I would very much like to understand in detail why this does not work.
Which (wrong) pid provides Popen?
Thanks in advance!
Related question:
Python on Windows - Issue with getting correct process id when opening file explorer programmatically

Is there a way to check what you are running a python program in?

I want to create a script for just Terminal and IDLE, but I don't know how. Using if 'idlelib' in sys.modules: works for seeing if it is running in IDLE, but is there a way to use the same code to find if it is in Terminal by replacing 'idlelib'?
You can try using psutil and os
import psutil
import os
if psutil.Process(os.getpid()).parent().name() in ["cmd.exe","bash"]:
print("in cmd")
Using idle it returned 'pythonw.exe' which shows this works.

In Python on Windows, how can I identify the current process ID using WMI or pywin32?

On Windows, using the WMI library, I can get a list of running Python programs like this
c = wmi.WMI()
for process in c.Win32_Process(name="python.exe"):
print(process.ProcessId, process.Name)
An example output is
21084 python.exe
10184 python.exe
12320 python.exe
How can I find out which of these processes is the currently running script?
I'm trying to use process.Terminate() on all the other Python scripts running,
because sometimes a Python script started by a GUI doesn't close. But I want to
avoid killing the script that does the cleanup - so I need a way of identifing it.
An easy way is to use os module to do that:
import os, wmi
c = wmi.WMI()
for process in c.Win32_Process(name="python.exe"):
print(process.ProcessId, process.Name)
print("current processId:", os.getpid())
Also you could use win32api of pywin32:
print("current processId:", win32api.GetCurrentProcessId())
I also run another script on my PC, this gave me:
17944 python.exe
10676 python.exe
current processId: 10676

Python automate open application

Please give me idea regarding how to tackle this problem. I am not able to find any resource regarding this. Your help will be immensely valuable. So we have one limited license software. And want to reiterate the python invoking the application. If the application gives the error that licence is not available it should close the application and wait for sometime say 1 min and again invoke the process, it should do so endlessly until a licence is available and the application is finally open.
I am able to open the application using
Import os
os.startfile('application executable')
After this I want the application to know if there is an error window popping , it should close the window and wait for sometime and again open the application
os.startfile returns as soon as the associated application is launch so use Popen instead.
As you are using windows use these steps.
To Open a Shortcut using Popen on Windows first install pywin32
Step one:
python -m pip install pywin32
Step two:
Navigate to your python Scrips folder something like
C:\Users\Name\AppData\Local\Programs\Python\Python38-32\Scripts then type the command.
pywin32_postinstall.py -install
Then the code to use Popen is.
import subprocess
import win32com.client, win32api
shell = win32com.client.Dispatch("WScript.Shell")
shortcut = shell.CreateShortCut(r'path to shortcut')
long_path = shortcut.Targetpath
p = subprocess.Popen(long_path)
p.wait()

Send SIGINT in Windows using Python

I try this code in Linux:
import os
import signal
for i in range(10000):
print i
if i==6666:
os.kill(os.getpid(),signal.SIGINT)
it works well. But it doesn't work in Windows, because the attribute 'kill' is not present in os module for Windows
How can I send SIGINT to self program in Windows?
from win32api import GenerateConsoleCtrlEvent
GenerateConsoleCtrlEvent(CTRL_C_EVENT, 0)

Categories

Resources