Fel*_*bek 5 python windows popen python-3.x mklink
我想为大型目录结构中的所有文件创建一个带符号链接的文件夹.我subprocess.call(["cmd", "/C", "mklink", linkname, filename])
首先使用它,它工作,但为每个符号链接打开一个新的命令窗口.
我无法弄清楚如何在没有窗口弹出的情况下在后台运行命令,所以我现在试图保持一个CMD窗口打开并通过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")
Run Code Online (Sandbox Code Playgroud)
哪里
cmdprocess = subprocess.Popen("cmd",
stdin = subprocess.PIPE,
stdout = subprocess.PIPE,
stderr = subprocess.PIPE)
Run Code Online (Sandbox Code Playgroud)
但是,我现在收到此错误:
File "mypythonfile.py", line 181, in makelink
cmdprocess.stdin.write("mklink " + linkname + " " + fullname + "\r\n")
TypeError: 'str' does not support the buffer interface
Run Code Online (Sandbox Code Playgroud)
这是什么意思,我该如何解决这个问题?
Python 字符串是 Unicode,但您要写入的管道仅支持字节。尝试:
cmdprocess.stdin.write(("mklink " + linkname + " " + fullname + "\r\n").encode("utf-8"))
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
6373 次 |
最近记录: |