I want to create symbolic links for each file in a nested directory structure, where all symbolic links will be placed in one large flat folder and now have the following code:
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)
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 reversal where the folder name is printed first, then subdirectories are processed. If I run it now on some folder, it all just stops after 10 or so symbolic links.
- , . 9 # tag & reencode ChillOutMix. cmd.exe , mklink ChillOutMix.
time.sleep(2) cmdprocess.stdin.write , Python cmd, .
- , ?