[python-win32] pywin32 programming error on Win7 with shell.SHGetDesktopFolder, desktop.BindToObject, desktop.GetDisplayNameOf

Tim Roberts timr at probo.com
Fri Nov 8 22:53:07 CET 2013


iMath wrote:
> When running the following code on WinXP , all is fine ,
> ...
> 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 ! 

The issue here is not XP versus Win 7.  The issue here is the Explorer
folder settings.  On your XP system, the user has selected the option to
show the file extensions.  On your Win 7 system, the user has NOT
selected that option.  You're using the GetDisplayNameOf API, which
returns to you the file name exactly as it would be displayed in
Explorer.  When that option is off, the filename "t.txt" would be
displayed as "t", so that's the key you are storing in
name_to_item_mapping.  The key "t.txt" is not found, as the error says.

What you're doing here is a little bit dangerous, because "t.xls" and
"t.txt" and "t.exe" will all return the same value from
GetDisplayNameOf.  You won't get a one-to-one assignment.  Since you
haven't explained what you actually expect this code to do, there's no
way for us to make a suggestion on a better method.

-- 
Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-win32/attachments/20131108/e6854531/attachment-0001.html>


More information about the python-win32 mailing list