[Tutor] making a shortcut in windows

Garry Willgoose Garry.Willgoose at newcastle.edu.au
Tue Sep 4 03:57:48 CEST 2012

I want to put a shortcut onto the desktop in windows (XP and later) in Python 2.6 or later. In Unix its easy using os.symlink but I can't find anything equivalent for windows. My searches on the web led me to the code below but the code returns the error

AttributeError: function 'CreateSymbolicLinkW' not found

so does anybody have any suggestions?

__CSL = None

def create_alias(source, linkname):
  Each operating system has a different way of creating an alias.  
  This is the windows version
  import os
    global __CSL
    if __CSL is None:
      import ctypes
      csl = ctypes.windll.kernel32.CreateSymbolicLinkW
      csl.argtypes = (ctypes.c_wchar_p, ctypes.c_wchar_p, ctypes.c_uint32)
      csl.restype = ctypes.c_ubyte
      __CSL = csl
    flags = 0
    if source is not None and os.path.isdir(source):
      flags = 1
    if __CSL(linkname, source, flags) == 0:
      raise ctypes.WinError()  
    print('#### Error creating a file alias from '+str(source)+' to '+str(linkname))

More information about the Tutor mailing list