繁体   English   中英

python - 在 windows 上引用网络中的路径时,删除了反斜杠

[英]python - backward slash is removed while referring to path in network on windows

netowrk_path = '\\shared_storage\test.txt'
cmd = f'/c copy {netowrk_path} D:\\temp'
print(cmd)

这在复制时打印并给出错误

/c copy \shared_storage test.txt D:\temp

但它实际上应该打印

/c copy \\shared_storage test.txt D:\temp

反斜杠 (\) 是一种转义某些字符的方法了解更多

您可以将字符串定义为原始字符串

string = r'\\somenetworkpath'
print(string)

output: \\somenetworkpath

阅读有关原始字符串的更多信息

您可以通过以下方式使用repr

print(repr(cmd))

这将为您提供所需的 output

您可以通过以下方式使用原始字符串和格式:

netowrk_path = r'\\shared_storage\test.txt'
cmd = rf'/c copy {netowrk_path} D:\\temp'
print(cmd)

您可以使用双斜杠\\替换所有单个\或使用 r 将其作为原始字符串,如上所示。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM