简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM