Python Windows CMD mklink,停止工作,没有错误消息
我想为嵌套目录结构中的每个文件创建符号链接,其中所有符号链接都将放入一个大的平面文件夹中,并且现在具有以下代码:
# loop over directory structure:
# for all items in current directory,
# if item is directory, recurse into it;
# else it's a file, then create a symlink for it
def makelinks(folder, targetfolder, cmdprocess = None):
if not cmdprocess:
cmdprocess = subprocess.Popen("cmd",
stdin = subprocess.PIPE,
stdout = subprocess.PIPE,
stderr = subprocess.PIPE)
print(folder)
for name in os.listdir(folder):
fullname = os.path.join(folder, name)
if os.path.isdir(fullname):
makelinks(fullname, targetfolder, cmdprocess)
else:
makelink(fullname, targetfolder, cmdprocess)
#for a given file, create one symlink in the target folder
def makelink(fullname, targetfolder, cmdprocess):
linkname = os.path.join(targetfolder, re.sub(r"[\/\\\:\*\?\"\<\>\|]", "-", fullname))
if not os.path.exists(linkname):
try:
os.remove(linkname)
print("Invalid symlink removed:", linkname)
except: pass
if not os.path.exists(linkname):
cmdprocess.stdin.write("mklink " + linkname + " " + fullname + "\r\n")
所以这是一个自上而下的递归,其中首先打印文件夹名称,然后处理子目录。如果我现在在某个文件夹上运行这个,整个事情就会在 10 个左右的符号链接后停止。
该程序似乎仍在运行,但没有生成新的输出。它为 # 标签中的某些文件创建了 9 个符号链接重新编码
以及ChillOutMix
文件夹中的前三个文件。 cmd.exe 窗口仍然打开且为空,并在其标题栏中显示它当前正在处理 ChillOutMix
中第三个文件的 mklink 命令。
我尝试在每个 cmdprocess.stdin.write
之后插入一个 time.sleep(2)
以防 Python 对于 cmd 进程来说太快,但这没有帮助。
有谁知道可能是什么问题?
I want to create symlinks for each file in a nested directory structure, where all symlinks will be put in one large flat folder, and have the following code by now:
# loop over directory structure:
# for all items in current directory,
# if item is directory, recurse into it;
# else it's a file, then create a symlink for it
def makelinks(folder, targetfolder, cmdprocess = None):
if not cmdprocess:
cmdprocess = subprocess.Popen("cmd",
stdin = subprocess.PIPE,
stdout = subprocess.PIPE,
stderr = subprocess.PIPE)
print(folder)
for name in os.listdir(folder):
fullname = os.path.join(folder, name)
if os.path.isdir(fullname):
makelinks(fullname, targetfolder, cmdprocess)
else:
makelink(fullname, targetfolder, cmdprocess)
#for a given file, create one symlink in the target folder
def makelink(fullname, targetfolder, cmdprocess):
linkname = os.path.join(targetfolder, re.sub(r"[\/\\\:\*\?\"\<\>\|]", "-", fullname))
if not os.path.exists(linkname):
try:
os.remove(linkname)
print("Invalid symlink removed:", linkname)
except: pass
if not os.path.exists(linkname):
cmdprocess.stdin.write("mklink " + linkname + " " + fullname + "\r\n")
So this is a top-down recursion where first the folder name is printed, then the subdirectories are processed. If I run this now over some folder, the whole thing just stops after 10 or so symbolic links.
The program still seems to run but no new output is generated. It created 9 symlinks for some files in the # tag & reencode
and the first three files in the ChillOutMix
folder. The cmd.exe Window is still open and empty, and shows in its title bar that it is currently processing the mklink command for the third file in ChillOutMix
.
I tried to insert a time.sleep(2)
after each cmdprocess.stdin.write
in case Python is just too fast for the cmd process, but it doesn't help.
Does anyone know what the problem might be?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么不直接执行mklink呢?
Why not just execute mklink directly?
最后试试这个:
看看它打印了什么命令。您可能会发现一个问题。
它可能需要在
mklink
的 arg 周围加上双引号,因为它有时包含空格。Try this at the end:
See what commands it prints. You may see a problem.
It may need double quotes around
mklink
's arg, since it contains spaces sometimes.