Symlinks on windows?

后端 未结 10 1334
轻奢々
轻奢々 2020-11-29 01:09

Does anyone know of a way to make/read symbolic links across versions of win32 from Python? Ideally there should be a minimum amount of platform specific code, as I need my

相关标签:
10条回答
  • 2020-11-29 01:49

    Using mklink command in subprocess create link.

    from subprocess import call
    call(['mklink', 'LINK', 'TARGET'], shell=True)
    
    0 讨论(0)
  • 2020-11-29 01:49

    Trying to create a Symlink in Windows I always got the error

    A required privilege is not held by the client
    

    However I was successfull when creating a shortcut with this code

    import win32com.client
    import pythoncom
    import os
    
    def create_shortcut(original_filepath, shortcut_filepath):
        shell = win32com.client.Dispatch("WScript.Shell")
        shortcut = shell.CreateShortCut(shortcut_filepath)
        shortcut.Targetpath = original_filepath
        shortcut.WindowStyle = 7
        shortcut.save()
    
    create_shortcut(r'C:\Users\xxx\Desktop\test.jpg', 
                    r'C:\Users\xxx\Desktop\test.lnk')
    

    Note: make sure the shortcut ends with '.lnk'

    0 讨论(0)
  • 2020-11-29 01:52

    here is the link containing all methods of kernel32.dll

    http://www.geoffchappell.com/studies/windows/win32/kernel32/api/

    I used CreateHardLinkA on Windows xp sp3, it worked!

    import ctypes if os.path.exists(link_file): os.remove(link_file)

    dll = ctypes.windll.LoadLibrary("kernel32.dll")
    dll.CreateHardLinkA(link_file, _log_filename, 0)
    
    0 讨论(0)
  • 2020-11-29 01:53

    Problem is, as explained e.g. here, that Windows' own support for the functionality of symbolic links varies across Windows releases, so that e.g. in Vista (with lots of work) you can get more functionality than in XP or 2000 (nothing AFAIK on other win32 versions). Or you could have shortcuts instead, which of course have their own set of limitations and aren't "really" equivalent to Unix symbolic links. So, you have to specify exactly what functionalities you require, how much of those you are willing to sacrifice on the altar of cross-win32 operation, etc -- THEN, we can work out how to implement the compromise you've chosen in terms of ctypes or win32all calls... that's the least of it, in a sense.

    0 讨论(0)
提交回复
热议问题