我想构建一个跨平台的应用程序,我使用了一个名为SHOpenFolderAndSelectItems()的Windows API。虽然我找到了pywin32所谓的例子,但是pywin32在Linux上不可用,我不想在linux上调用Windows API,只是不想为Linux制作另一个代码版本,所以我想知道如何访问它通过ctypes?是的,这个API不能在Linux上调用,我只是想让它在代码中保持静默,这样我就可以通过cx_Freeze将Python脚本冻结成可执行文件,而不会发生pywin32模块缺失错误。
from win32com.shell import shell, shellcon
import os
def launch_file_explorer(path, files):
'''
Given a absolute base path and names of its children (no path), open
up one File Explorer window with all the child files selected
'''
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, shellcon.SHGDN_FORPARSING|shellcon.SHGDN_INFOLDER), item) for item in shell_folder])
print(name_to_item_mapping)
to_show = []
for file in files:
if file in name_to_item_mapping:
to_show.append(name_to_item_mapping[file])
# else:
# raise Exception('File: "%s" not found in "%s"' % (file, path))
shell.SHOpenFolderAndSelectItems(folder_pidl, to_show, 0)
p=r'E:\aa'
print(os.listdir(p))
launch_file_explorer(p, os.listdir(p))