Python Windows CMD mklink,停止工作没有错误消息 [英] Python Windows CMD mklink, stops working without error message

查看:582
本文介绍了Python Windows CMD mklink,停止工作没有错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为嵌套目录结构中的每个文件创建符号链接,其中所有符号链接将放在一个大的平面文件夹中,现在有以下代码:

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")

所以这是一个自上而下的递归,打印文件夹名称,然后处理子目录。如果我现在运行这个文件夹,整个事情只是停止后10左右的符号链接。

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.

程序似乎仍在运行,但不会生成新的输出。它为#标签中的一些文件创建了9个符号链接, reencode ChillOutMix 文件夹中的前三个文件。 cmd.exe窗口仍然打开并且为空,并在其标题栏中显示它当前正在处理 ChillOutMix 中的第三个文件的mklink命令。

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.

我试图在每个 cmdprocess.stdin.write time.sleep(2) c / c>

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?

推荐答案

最后尝试:

if not os.path.exists(linkname):
    fullcmd = "mklink " + linkname + " " + fullname + "\r\n"
    print fullcmd
    cmdprocess.stdin.write(fullcmd)

查看它打印的命令。您可能会遇到问题。

See what commands it prints. You may see a problem.

mklink 的arg可能需要双引号,因为它包含空格。

It may need double quotes around mklink's arg, since it contains spaces sometimes.

这篇关于Python Windows CMD mklink,停止工作没有错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆