如何使用Python在Windows中创建符号链接?

gab*_*iel 2 python directory symlink ctypes operating-system

我试图symlinks在Windows 8上使用Python 创建.我发现了这篇帖子,这是我脚本的一部分.

import os

link_dst = unicode(os.path.join(style_path, album_path))
link_src = unicode(album_path)
kdll = ctypes.windll.LoadLibrary("kernel32.dll")
kdll.CreateSymbolicLinkW(link_dst, link_src, 1)
Run Code Online (Sandbox Code Playgroud)

首先,它只有在通过管理员cmd执行时才能创建符号链接.为什么会这样?

其次,当我试图从Windows资源管理器中打开这些符号链接时,我得到此错误:

...Directory is not accessible. The Name Of The File Cannot Be Resolved By The System.
Run Code Online (Sandbox Code Playgroud)

有没有更好的方法使用Python创建符号链接?如果没有,我该如何解决?

编辑

这是foralbum_linker中的循环:

def album_Linker(album_path, album_Genre, album_Style):
genre_basedir = "E:\Music\#02.Genre"
artist_basedir = "E:\Music\#03.Artist"
release_data_basedir = "E:\Music\#04.ReleaseDate"
for genre in os.listdir(genre_basedir):
    genre_path = os.path.join(genre_basedir, "_" + album_Genre)
    if not os.path.isdir(genre_path):
        os.mkdir(genre_path)
    album_Style_list = album_Style.split(', ')
    print album_Style_list
    for style in album_Style_list:
        style_path = os.path.join(genre_path, "_" + style)
        if not os.path.isdir(style_path):
            os.mkdir(style_path)
        album_path_list = album_path.split("_")
        print album_path_list
        #link_dst = unicode(os.path.join(style_path, album_path_list[2] + "_" + album_path_list[1] + "_" + album_path_list[0]))
        link_dst = unicode(os.path.join(style_path, album_path))
        link_src = unicode(album_path)
        kdll = ctypes.windll.LoadLibrary("kernel32.dll")
        kdll.CreateSymbolicLinkW(link_dst, link_src, 1)
Run Code Online (Sandbox Code Playgroud)

这需要album_Genrealbum_Style,然后将其下创建目录E:\Music\#02.Genre.它还需要来自脚本主体的album_path.这个album_path是我想在其下创建符号链接的目录的路径E:\Music\#02.Genre\Genre\Style.所以album_path是从for脚本主体中的另一个循环中获取的变量

for label in os.listdir(basedir):
label_path = os.path.join(basedir, label)
for album in os.listdir(label_path):
    album_path = os.path.join(label_path, album)
    if not os.path.isdir(album_path):
        # Not A Directory
        continue
    else:
        # Is A Directory
        os.mkdir(os.path.join(album_path + ".copy"))
        # Let Us Count
        j = 1
        z = 0
        # Change Directory
        os.chdir(album_path)
Run Code Online (Sandbox Code Playgroud)

tde*_*ney 5

首先,它只有在通过管理员cmd执行时才能创建符号链接.

用户需要"创建符号链接"权限才能创建符号链接.默认情况下,普通用户没有,但管理员没有.改变这种情况的一种方法是使用安全策略编辑器.以管理员身份打开命令提示符,运行secpol.msc然后转到Security Settings\Local Policies\User Rights Assignment\Create symbolic links进行更改.

其次,当我试图从Windows资源管理器中打开这些符号链接时,我得到此错误:

您没有转义文件名中的反斜杠.只需在原始字符串的前面添加"r",文件名就会改变.您正在设置一个不存在的文件名,因此资源管理器找不到它.

>>> link_dst1 = "E:\Music\#02.Genre_Electronic_Bass Music\1-800Dinosaur-1-800-001_[JamesBlake-Voyeur(Dub)AndHolyGhost]_2013-05-00"
>>> link_dst2 = r"E:\Music\#02.Genre_Electronic_Bass Music\1-800Dinosaur-1-800-001_[JamesBlake-Voyeur(Dub)AndHolyGhost]_2013-05-00"
>>> link_dst1 == link_dst2
False
>>> print link_dst1
E:\Music\#02.Genre_Electronic_Bass Music?-800Dinosaur-1-800-001_[JamesBlake-Voyeur(Dub)AndHolyGhost]_2013-05-00
Run Code Online (Sandbox Code Playgroud)


Bla*_*ift 5

os.symlink从 python 3.8 开始,只要打开开发者模式,就可以在 Windows 上开箱即用。