python - 如何在 Windows 上打开包含选定项目的文件夹

标签 python windows winapi python-2.7 python-3.x

我想打开一个文件夹,其中包含在 Windows 上选择的指定项目。我查找了 Windows Shell 引用并找到了适合此工作的函数:SHOpenFolderAndSelectItems .

但是,我找不到有关如何在 Python 中使用它的示例。有人知道我该怎么做吗?

我还有另一个额外要求:如果该文件夹已打开,请不要再次打开它,只需激活它并选择文件即可。

最佳答案

使用 PyWin32 您可以执行类似的操作,默认情况下它应该激活并选择文件(如果已打开):

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

关于python - 如何在 Windows 上打开包含选定项目的文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16108503/

相关文章:

python - 从 python Gtk3 获取 gtk 主题 css 属性

Windows 8 和 Windows 电话 8

c - 如何使用 win32API 在 ListView 中选择一行

c++ - WinXP下如何控制应用程序音量

python - shutil make_archive 生成​​嵌套的 .zip 文件

python - 使用Python Selenium Chromedriver下载不一致 'Failed - Path too long'错误

python - Numpy 在转置矩阵时如何移动数据?

c++ - 开发使用 MS CryptoAPI 的 64 位应用程序

windows - 如何在通过文件 ://protocol? 指定的 Remote 上正确设置 Hook

c++ - 在我设置“视觉样式”后,常用控件未正确用WM_CTLCOLORSTATIC处理程序绘制