【发布时间】:2011-07-12 07:29:18
【问题描述】:
我想创建一个包含指向大型目录结构中所有文件的符号链接的文件夹。我首先使用了subprocess.call(["cmd", "/C", "mklink", linkname, filename]),它确实有效,但是为每个符号链接打开了一个新的命令窗口。
我无法弄清楚如何在不弹出窗口的情况下在后台运行命令,所以我现在尝试保持一个 CMD 窗口打开并通过标准输入在那里运行命令:
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")
在哪里
cmdprocess = subprocess.Popen("cmd",
stdin = subprocess.PIPE,
stdout = subprocess.PIPE,
stderr = subprocess.PIPE)
但是,我现在收到此错误:
File "mypythonfile.py", line 181, in makelink
cmdprocess.stdin.write("mklink " + linkname + " " + fullname + "\r\n")
TypeError: 'str' does not support the buffer interface
这是什么意思,我该如何解决?
【问题讨论】:
标签: python windows python-3.x popen mklink