Yet another Python Windows CMD mklink problem ... can't get it to work!
Posted
by
Felix Dombek
on Stack Overflow
See other posts from Stack Overflow
or by Felix Dombek
Published on 2011-03-09T23:54:10Z
Indexed on
2011/03/10
0:10 UTC
Read the original article
Hit count: 373
OK I have just posted another question which outlined my program but the specific problem was different.
Now, my program just stops working without any message whatsoever. I'd be grateful if someone could help me here.
I want to create symlinks for each file in a directory structure, all 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. Here is the output:
D:\Musik\neu
D:\Musik\neu\# Electronic
D:\Musik\neu\# Electronic\# tag & reencode
D:\Musik\neu\# Electronic\# tag & reencode\ChillOutMix
D:\Musik\neu\# Electronic\# tag & reencode\Unknown D&B
D:\Musik\neu\# Electronic\# tag & reencode\Unknown D&B 2
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?
© Stack Overflow or respective owner