2024-04-25 13:04:40 发布
网友
我一定是做错了。在
s.sendall((str("mkd /" + folder.decode() + "/New Folder/ \r\n")).encode())
有没有更好的方式来写这个?在
s.sendall(b"mkd '/" + folder + b"/New Folder/'\r\n")
下面是一个更健壮的版本,它使用了python3.3中的shlex.quote()(在较早的Python版本上使用pipes.quote()):
shlex.quote()
pipes.quote()
后者假设folder是Unicode字符串。如果folder是一个bytes对象,那么您不应该盲目地使用folder.decode(),因为不能保证folder被编码为utf-8。是否可以使用os.fsdecode()对其进行解码取决于folder来自何处。在
folder
folder.decode()
os.fsdecode()
下面是一个更健壮的版本,它使用了python3.3中的
^{pr2}$shlex.quote()
(在较早的Python版本上使用pipes.quote()
):后者假设
folder
是Unicode字符串。如果folder
是一个bytes对象,那么您不应该盲目地使用folder.decode()
,因为不能保证folder
被编码为utf-8。是否可以使用os.fsdecode()
对其进行解码取决于folder
来自何处。在相关问题 更多 >
编程相关推荐