【发布时间】:2015-01-03 10:55:55
【问题描述】:
我正在尝试在 Windows 8 上使用 Python 创建symlinks。我找到了This Post,这是我脚本的一部分。
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执行才能创建符号链接。为什么会这样?
其次,当我尝试从 Windows 资源管理器中打开这些符号链接时,我收到此错误:
...Directory is not accessible. The Name Of The File Cannot Be Resolved By The System.
有没有更好的使用 Python 创建符号链接的方法?如果没有,我该如何解决?
编辑
这是专辑链接器中的for 循环:
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 是我要在 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)
【问题讨论】:
-
这个answer 可能会有所帮助
-
你的路径是什么? link_dst, link_src
-
@Andy 我试过了,我从导入中得到了未定义的变量:CreateSymbolicLinkW
-
@User 此代码存在于
for循环中。所以路径正在改变。例如 link_dst = "E:\Music\#02.Genre_Electronic_Bass Music\1-800Dinosaur-1-800-001_[JamesBlake-Voyeur(Dub)AndHolyGhost]_2013-05-00" and link_dst = "E:\Music\ #01.Label_1-800Dinosaur\1-800Dinosaur-1-800-001_[JamesBlake-Voyeur(Dub)AndHolyGhost]_2013-05-00" -
如果你可以使用 Python 3,那就是
os.symlink。
标签: python operating-system directory ctypes symlink