I want to open a folder with specified items selected on Windows. I looked up the the Windows Shell Reference and found a function fit for this job: SHOpenFolderAndSelectItems.
However, I couldn't find an example on how to use it with Python. Does anybody know how I can do this?
I have another extra requirement: if that folder already open, don’t open it again and just activate it and select the file.
Using PyWin32 you can do something such as this, by default it should just activate and select the file if already open:
from win32com.shell import shell, shellcon import win32api folder = win32api.GetTempPath() folder_pidl=shell.SHILCreateFromPath(folder,0) desktop = shell.SHGetDesktopFolder() shell_folder = desktop.BindToObject(folder_pidl, None, shell.IID_IShellFolder) items = [item for item in shell_folder][:5] ## print (items) shell.SHOpenFolderAndSelectItems(folder_pidl, items, 0)
Maybe you could try running the shell command through Python using subprocess.Popen. Check out this thread for more info: How to use subprocess popen Python