Symlinks on windows?

后端 未结 10 1510
轻奢々
轻奢々 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:37

    NTFS file system has junction points, I think you may use them instead, You can use python win32 API module for that e.g.

    import win32file
    
    win32file.CreateSymbolicLink(fileSrc, fileTarget, 1)
    

    If you do not want to rely on win32API module, you can always use ctypes and directly call CreateSymbolicLink win32 API e.g.

    import ctypes
    
    kdll = ctypes.windll.LoadLibrary("kernel32.dll")
    
    kdll.CreateSymbolicLinkA("d:\\test.txt", "d:\\test_link.txt", 0)
    

    MSDN (http://msdn.microsoft.com/en-us/library/aa363866(VS.85).aspx) says Minimum supported client is Windows Vista

    In addition: This also works with directories (indicate that with the third argument). With unicode support it looks like this:

    kdll.CreateSymbolicLinkW(UR"D:\testdirLink", UR"D:\testdir", 1)
    

    also see Create NTFS junction point in Python

提交回复
热议问题