如何使用Python在Windows中创建符号链接? [英] How to create symlinks in windows using Python?
问题描述
我试图在Windows 8上使用Python创建symlinks
.我发现了这篇文章和这是我脚本的一部分.
I am trying to create symlinks
using Python on Windows 8. I found This Post and this is part of my script.
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)
首先,它只有在通过管理员cmd执行后才能创建符号链接.为什么会这样?
Firstly, It can create symlinks only when it is executed through administrator cmd. Why is that happening?
第二,当我尝试从Windows资源管理器中打开这些符号链接时,出现此错误:
Secondly, When I am trying to open those symlinks from windows explorer I get This Error:
...Directory is not accessible. The Name Of The File Cannot Be Resolved By The System.
是否有使用Python创建符号链接的更好方法?如果没有,我该如何解决?
编辑
这是album_linker中的for
循环:
This is the for
loop in album_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)
使用album_Genre
和album_Style
,然后在E:\Music\#02.Genre
下创建目录.它还从脚本主体中获取album_path.这个album_path是我要在E:\Music\#02.Genre\Genre\Style
下创建符号链接的目录路径.因此,album_path是从脚本主体中的另一个for
循环获取的变量
It takes album_Genre
and album_Style
And then It creates directories under E:\Music\#02.Genre
. It also takes album_path from the main body of the script. This album_path is the path of directory which i want to create the symlink under E:\Music\#02.Genre\Genre\Style
. So album_path is a variable taken from another for
loop in the main body of the script
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)
推荐答案
首先,它只有在通过管理员cmd执行后才能创建符号链接.
用户需要创建符号链接"权限才能创建符号链接.默认情况下,普通用户没有,但管理员有.一种更改方法是使用安全策略编辑器.以管理员身份打开命令提示符,运行secpol.msc
,然后转到Security Settings\Local Policies\User Rights Assignment\Create symbolic links
进行更改.
Users need "Create symbolic links" rights to create a symlink. By default, normal users don't have it but administrator does. One way to change that is with the security policy editor. Open a command prompt as administrator, run secpol.msc
and then go to Security Settings\Local Policies\User Rights Assignment\Create symbolic links
to make the change.
第二,当我尝试从Windows资源管理器中打开这些符号链接时,出现此错误:
您没有在文件名中转义反斜杠.只需在原始字符串的前面添加"r",文件名便会更改.您正在设置一个不存在的文件名,因此资源管理器找不到它.
You aren't escaping the backslashes in the file name. Just by adding an "r" to the front for a raw string, the file name changes. You are setting a non-existant file name and so explorer can't find it.
>>> 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
这篇关于如何使用Python在Windows中创建符号链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!