【问题标题】:How to open folder with specified items selected on Windows如何打开在 Windows 上选择了指定项目的文件夹
【发布时间】:2013-04-13 01:49:33
【问题描述】:

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

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

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

【问题讨论】:

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


    【解决方案1】:

    使用 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

    【讨论】:

    【解决方案2】:

    也许您可以尝试使用 subprocess.Popen 通过 Python 运行 shell 命令。 查看此线程以获取更多信息:How to use subprocess popen Python

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多