I have seen answers about how to create symbolic links on Windows using Python (I am on Win 7). However the links does not show up in the system for some reason even though the call claim success. Am I missing something obvious ?
>>> import ctypes
>>> kdll = ctypes.windll.LoadLibrary("kernel32.dll")
>>> kdll.CreateSymbolicLinkA("C:\\testlink", "C:\\existing_dir", 1)
1
>>> ctypes.WinError()
WindowsError(0, 'The operation completed successfully.')
>>> kdll.CreateSymbolicLinkA("C:\\testlink", "C:\\existing_dir", 1)
0
>>> ctypes.WinError()
WindowsError(0, 'Cannot create a file when that file already exists.')
>>> os.path.exists("C:\\testlink")
False
>>> os.path.islink("C:\\testlink")
False
Why does the calls to the windows api think that that the link was created when python can't see it with os.path.exist ? And neither can I see the link in the file explorer or using dir
in cmd on the Windows box. So where did the link go ?
call(['mklink', 'C:\\testlink', 'C:\\existing_dir'], shell=True)
. Still that does not explain why I see the behavior above - so I am still curious. – GamophyllousSeCreateSymbolicLinkPrivilege
, and it says that it did work. Did you try creating the link in another directory, such asC:\Temp
? If you're using 32-bit Python, maybe it was created in%LOCALAPPDATA%\VirtualStore
. – Cordeiromklink
command creates a broken link. You needmklink /d
to link to a directory. Trying to open a file as a directory fails. This is checked in theNtOpenFile
system call before the path is reparsed to follow the symbolic link, so the actual link itself needs to be created as a directory. That's why yourCreateSymbolicLink
call uses the flagSYMBOLIC_LINK_FLAG_DIRECTORY
(1). – Cordeiro