Unicode-aware file shortcuts in Windows

Stanislaw Findeisen my_initials at nglogic.com
Sat Sep 17 01:19:10 CEST 2005

Does anyone know how to create file shortcuts in Windows?

The only way I know is like:


import win32com.client

wScriptShellObject = win32com.client.Dispatch("WScript.Shell")
shortcutName = unicode("shortcut.lnk", "utf8")
shortcut = wScriptShellObject.CreateShortcut(shortcutName)
shortcut.TargetPath = <FULL PATH TO TARGET FILE>


Unfortunately, this causes problems with Unicode characters:

(1) If there are Unicode characters in target file name, not all of them 
get correctly copied to the shortcut's "target" attribute, thus 
producing an invalid shortcut.

(2) If there are Unicode characters in shortcut name, not all of them 
get correctly copied, thus producing a shortcut with a name different 
than desired.

(3) If there are Unicode characters somewhere in the path to the 
shortcut itself (above the shortcut name component), then they also get 
corrupted, which effects in an invalid (non-existent) path and errors 
like this one:


E:\Documents and Settings\Staszek\Progs\Python-Windows\test_1>cf.py
Traceback (most recent call last):
   File "E:\Documents and 
Settings\Staszek\Progs\Python-Windows\test_1\cf.py", line 7, in ?
   File "<COMObject <unknown>>", line 2, in Save
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, 
'WshShortcut.Save', 'Unable to save shortcut "E:\\Documents and 
Settings\\Staszek\\Progs\\Python-Windows\\test_1\\Te\x9a\xed me c\xfd 
z\xf3lazcseL\\link do vecer Z\xd3LAKe e\x9a\xed bla.lnk".', None, 0, 
-2147024893), None)


The original path to shortcut file referenced in error description above 
contains Polish and Czech characters, and it was:


E:\Documents and Settings\Staszek\Progs\Python-Windows\test_1\Těší mě čý 
żółąźćśęŁ\link do večer ŻÓŁĄKę ěší bla.lnk


As it can be seen, some of those national characters in the error 
description string are escaped using the \x.. sequence, while others are 
simply "flatten" (like first "e" in "Te\x9a\xed", or "lazcse").

So it seems as if WScript.Shell object in Python didn't fully support 
Unicode?... Or perhaps it does, but one has to know how to activate it.

Any help would be appreciated.

I am using standard Windows console cmd.exe in Windows 2000, Python 2.4, 
and Mark Hammond's Python for Windows Extensions build 204.

| When replying, please replace "my_initials" in the    |
| From: address field with my initials - that is, "SF". |

     Enter through the narrow gate! (Mt 7:13-14)

More information about the Python-list mailing list