5

I want to create a folder with symlinks to all files in a large directory structure. I used subprocess.call(["cmd", "/C", "mklink", linkname, filename]) first, and it worked, but opened a new command windows for each symlink.

I couldn't figure out how to run the command in the background without a window popping up, so I'm now trying to keep one CMD window open and run commands there via stdin:

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

where

cmdprocess = subprocess.Popen("cmd",
                              stdin  = subprocess.PIPE,
                              stdout = subprocess.PIPE,
                              stderr = subprocess.PIPE)

However, I now get this error:

File "mypythonfile.py", line 181, in makelink
cmdprocess.stdin.write("mklink " + linkname + " " + fullname + "\r\n")
TypeError: 'str' does not support the buffer interface

What does this mean and how can I solve this?

Felix Dombek
  • 13,664
  • 17
  • 79
  • 131

1 Answers1

1

Python strings are Unicode, but the pipe you're writing to only supports bytes. Try:

cmdprocess.stdin.write(("mklink " + linkname + " " + fullname + "\r\n").encode("utf-8"))
Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
  • Ah. That was that, thank you. Now this whole thing just stops working after 10 or so files ... maybe you know something for that as well? I made a new question at http://stackoverflow.com/questions/5253835/yet-another-python-windows-cmd-mklink-problem-cant-get-it-to-work THX – Felix Dombek Mar 09 '11 at 23:57