pywin32 programming error on Win7 with shell.SHGetDesktopFolder,desktop.BindToObject,desktop.GetDisp

I

iMath

When running the following code on WinXP , all is fine ,
--------------------------------------------------------------
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))
--------------------------------------------------------------

however ,when tested on Win7,I got the following error

--------------------------------------------------------------

['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"
 
T

Tim Golden

When running the following code on WinXP , all is fine ,
--------------------------------------------------------------
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))


Did you mean to call .GetDisplayNameOf against the desktop folder? I
would have thought you wanted shell_folder.GetDisplayNameOf(item, 0).

TJG
 
I

iMath

在 2013å¹´11月8日星期五UTC+8下åˆ5æ—¶47分43秒,Tim Golden写é“:
When running the following code on WinXP , all is fine ,
--------------------------------------------------------------

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




launch_file_explorer(p, os.listdir(p))





Did you mean to call .GetDisplayNameOf against the desktop folder? I

would have thought you wanted shell_folder.GetDisplayNameOf(item, 0).



TJG

I mainly want to use shell.SHOpenFolderAndSelectItems()
 
I

iMath

在 2013å¹´11月8日星期五UTC+8下åˆ5æ—¶47分43秒,Tim Golden写é“:
When running the following code on WinXP , all is fine ,
--------------------------------------------------------------

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




launch_file_explorer(p, os.listdir(p))





Did you mean to call .GetDisplayNameOf against the desktop folder? I

would have thought you wanted shell_folder.GetDisplayNameOf(item, 0).



TJG

I mainly want to use shell.SHOpenFolderAndSelectItems()
 
M

MRAB

When running the following code on WinXP , all is fine ,
--------------------------------------------------------------
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))
--------------------------------------------------------------

however ,when tested on Win7,I got the following error

--------------------------------------------------------------

['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"
It's building a dict where the key is the result of
desktop.GetDisplayNameOf(item, 0), and it's then not finding a given
name in that dict.

Try printing the keys to see what's there. It might be that
desktop.GetDisplayNameOf(item, 0) returns different strings in Win7
from those in WinXP.
 
I

iMath

thanks, I found out the reason: this question has no relationship with Win7
or WinXP,but related to the users' system setting .
GetDisplayNameOf() only gives the DISPLAY name ,so the filename with or without extension acording to whether the user want to view files in the explorer with extension .
my settings is that filename with extension on WinXP but not on Win7,while os.listdir() returns filenames with extension on both of them ,this caused my problem here .
so anyone know how to fix my problem ? I mainly want to use shell.SHOpenFolderAndSelectItems() here.
 
I

iMath

I solved it like the following

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))
 
I

iMath

I solved it like the following

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))
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top