问题描述:

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)[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)

http://mail.python.org/pipermail/python-win32/2012-September/012531.html

网友答案:

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

相关阅读:
Top