1

在 WinXP 上运行以下代码时,一切正常,

from win32com.shell import shell

def launch_file_explorer(path, files):

    folder_pidl = shell.SHILCreateFromPath(path,0)[0]
    desktop = shell.SHGetDesktopFolder()
    shell_folder = desktop.BindToObject(folder_pidl, None,shell.IID_IShellFolder)
    name_to_item_mapping = dict([(desktop.GetDisplayNameOf(item, 0), item) for item in shell_folder])
    to_show = []
    for file in files:
        if file not in name_to_item_mapping:
            raise Exception('File: "{}" not found in "{}"'.format(file, path))
        to_show.append(name_to_item_mapping[file])
    shell.SHOpenFolderAndSelectItems(folder_pidl, to_show, 0)
import os


p=r'E:\aa'
print(os.listdir(p))
launch_file_explorer(p, os.listdir(p))

但是,在Win7上测试时,出现以下错误

['b1', 'b2', 't.txt']
Traceback (most recent call last):
  File "D:\g.py", line 21, in <module>
    launch_file_explorer(p, os.listdir(p))
  File "D:\g.py", line 13, in launch_file_explorer
    raise Exception('File: "{}" not found in "{}"'.format(file, path))
Exception: File: "t.txt" not found in "E:\aa"

我几乎没有 pywin32 编程经验,上面的代码是用谷歌搜索的,任何人都可以帮我解决这个错误吗?提前谢谢!

4

0 回答 0