'Check if Windows File Explorer is already opened in python
I have simple script that launches Windows File Explorer
import subprocess
subprocess.call(["start", "explorer.exe"],shell=True)
I'd like to check if Windows File Explorer is already opened so that I don't open another instance. How can I do it? Solutions without external libraries are preferred.
Solution 1:[1]
Found the solution
import win32gui
explorerWindows = []
def handler( hwnd, list ):
# only explorer windows have class 'CabinetWClass'
if 'CabinetWClass' in win32gui.GetClassName(hwnd):
list.append(hwnd)
win32gui.EnumWindows(handler, explorerWindows)
explorerOpened = len(explorerWindows) > 0
EDIT: easier method
import win32gui
explorerOpened = win32gui.FindWindow('CabinetWClass', None) != 0
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |