iMath
iMath

Reputation: 2476

How to open folder with specified items selected on Windows

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.

Upvotes: 2

Views: 1058

Answers (2)

Jammerx2
Jammerx2

Reputation: 864

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

Upvotes: 3

user2286078
user2286078

Reputation:

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

Upvotes: 1

Related Questions