Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #58745 > unrolled thread

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

Started byiMath <redstone-cold@163.com>
First post2013-11-07 19:30 -0800
Last post2013-11-10 04:39 -0800
Articles 8 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  pywin32 programming error on Win7 with shell.SHGetDesktopFolder,desktop.BindToObject,desktop.GetDisplayNameOf iMath <redstone-cold@163.com> - 2013-11-07 19:30 -0800
    Re: pywin32 programming error on Win7 with shell.SHGetDesktopFolder,  desktop.BindToObject, desktop.GetDisplayNameOf Tim Golden <mail@timgolden.me.uk> - 2013-11-08 09:47 +0000
      Re: pywin32 programming error on Win7 with shell.SHGetDesktopFolder,  desktop.BindToObject, desktop.GetDisplayNameOf iMath <redstone-cold@163.com> - 2013-11-08 07:28 -0800
      Re: pywin32 programming error on Win7 with shell.SHGetDesktopFolder,  desktop.BindToObject, desktop.GetDisplayNameOf iMath <redstone-cold@163.com> - 2013-11-08 07:28 -0800
    Re: pywin32 programming error on Win7 with shell.SHGetDesktopFolder,  desktop.BindToObject, desktop.GetDisplayNameOf MRAB <python@mrabarnett.plus.com> - 2013-11-08 17:51 +0000
      Re: pywin32 programming error on Win7 with shell.SHGetDesktopFolder,  desktop.BindToObject, desktop.GetDisplayNameOf iMath <redstone-cold@163.com> - 2013-11-09 21:23 -0800
    Re: pywin32 programming error on Win7 with shell.SHGetDesktopFolder,desktop.BindToObject,desktop.GetDisplayNameOf iMath <redstone-cold@163.com> - 2013-11-10 04:39 -0800
    Re: pywin32 programming error on Win7 with shell.SHGetDesktopFolder,desktop.BindToObject,desktop.GetDisplayNameOf iMath <redstone-cold@163.com> - 2013-11-10 04:39 -0800

#58745 — pywin32 programming error on Win7 with shell.SHGetDesktopFolder,desktop.BindToObject,desktop.GetDisplayNameOf

FromiMath <redstone-cold@163.com>
Date2013-11-07 19:30 -0800
Subjectpywin32 programming error on Win7 with shell.SHGetDesktopFolder,desktop.BindToObject,desktop.GetDisplayNameOf
Message-ID<1b6591af-904c-4a95-8a7b-ee0c63af75db@googlegroups.com>
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"
--------------------------------------------------------------
I nearly have no experience on pywin32 programming ,the above code is googled,anyone can help me fix this error ?thanks in advance !

[toc] | [next] | [standalone]


#58771 — Re: pywin32 programming error on Win7 with shell.SHGetDesktopFolder, desktop.BindToObject, desktop.GetDisplayNameOf

FromTim Golden <mail@timgolden.me.uk>
Date2013-11-08 09:47 +0000
SubjectRe: pywin32 programming error on Win7 with shell.SHGetDesktopFolder, desktop.BindToObject, desktop.GetDisplayNameOf
Message-ID<mailman.2200.1383904073.18130.python-list@python.org>
In reply to#58745
On 08/11/2013 03:30, iMath wrote:
> 
> 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

[toc] | [prev] | [next] | [standalone]


#58804 — Re: pywin32 programming error on Win7 with shell.SHGetDesktopFolder, desktop.BindToObject, desktop.GetDisplayNameOf

FromiMath <redstone-cold@163.com>
Date2013-11-08 07:28 -0800
SubjectRe: pywin32 programming error on Win7 with shell.SHGetDesktopFolder, desktop.BindToObject, desktop.GetDisplayNameOf
Message-ID<3da65462-2d13-40c6-9a5d-29f962d4828d@googlegroups.com>
In reply to#58771
在 2013年11月8日星期五UTC+8下午5时47分43秒,Tim Golden写道:
> On 08/11/2013 03:30, iMath wrote:
> 
> > 
> 
> > 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 mainly want to use shell.SHOpenFolderAndSelectItems()

[toc] | [prev] | [next] | [standalone]


#58805 — Re: pywin32 programming error on Win7 with shell.SHGetDesktopFolder, desktop.BindToObject, desktop.GetDisplayNameOf

FromiMath <redstone-cold@163.com>
Date2013-11-08 07:28 -0800
SubjectRe: pywin32 programming error on Win7 with shell.SHGetDesktopFolder, desktop.BindToObject, desktop.GetDisplayNameOf
Message-ID<119e2d8a-7ee4-4715-a636-e52c4849b148@googlegroups.com>
In reply to#58771
在 2013年11月8日星期五UTC+8下午5时47分43秒,Tim Golden写道:
> On 08/11/2013 03:30, iMath wrote:
> 
> > 
> 
> > 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 mainly want to use shell.SHOpenFolderAndSelectItems()

[toc] | [prev] | [next] | [standalone]


#58825 — Re: pywin32 programming error on Win7 with shell.SHGetDesktopFolder, desktop.BindToObject, desktop.GetDisplayNameOf

FromMRAB <python@mrabarnett.plus.com>
Date2013-11-08 17:51 +0000
SubjectRe: pywin32 programming error on Win7 with shell.SHGetDesktopFolder, desktop.BindToObject, desktop.GetDisplayNameOf
Message-ID<mailman.2233.1383933110.18130.python-list@python.org>
In reply to#58745
On 08/11/2013 03:30, iMath wrote:
>
> 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"
> --------------------------------------------------------------
> I nearly have no experience on pywin32 programming ,the above code is googled,anyone can help me fix this error ?thanks in advance !
>
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.

[toc] | [prev] | [next] | [standalone]


#58985 — Re: pywin32 programming error on Win7 with shell.SHGetDesktopFolder, desktop.BindToObject, desktop.GetDisplayNameOf

FromiMath <redstone-cold@163.com>
Date2013-11-09 21:23 -0800
SubjectRe: pywin32 programming error on Win7 with shell.SHGetDesktopFolder, desktop.BindToObject, desktop.GetDisplayNameOf
Message-ID<8af729fe-d7e4-4edc-88e2-b5f2919796fc@googlegroups.com>
In reply to#58825
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.

[toc] | [prev] | [next] | [standalone]


#59003

FromiMath <redstone-cold@163.com>
Date2013-11-10 04:39 -0800
Message-ID<f61d2a8c-35e0-4b17-869b-a1114808ce5d@googlegroups.com>
In reply to#58745
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))

[toc] | [prev] | [next] | [standalone]


#59004

FromiMath <redstone-cold@163.com>
Date2013-11-10 04:39 -0800
Message-ID<35f23753-e663-43dc-b758-f7ca79114bde@googlegroups.com>
In reply to#58745
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))

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web