为什么文件路径的输出中反斜杠加倍?
我对这个脚本有疑问。在输出中,它加倍 \\
。
import os
import time
import shutil
login = os.getlogin()
print(login)
while True:
filepath = input()
if filepath == 'end':
break
elif filepath[0:4] == 'copy': #im start command with 'copy' to copy file like 'copy C:\\Users...'
dirlist = os.listdir(filepath[5::])
print('Type dir to copied file')
codir = input('copy dir is..\n')
shutil.copy(filepath[5::], codir)
elif filepath[0:6] == 'search': #im start command with 'search' to copy file like 'search C:\\Users...'
print(filepath[6:-1])
dirlist = os.listdir(filepath[6::])
searchfile = input('file you want to search\n')
if searchfile in dirlist:
print("True")
else:
dirlist = os.listdir(filepath) # if it has no command, it's just show dir list
print(dirlist)
print('Script end!')
输出:
$ Tester.py
Artem
С:\\\Users
Traceback (most recent call last):
File "C:\Users\Artem\Desktop\Tester.py", line 24, in <module>
FileNotFoundError: [WinError 3] Системе не удается найти указанный путь: 'С:\\\\Users'
^System can't reach this way:'C:\\\\Users'
为什么它是双倍的\\
?
I have a problem with this script. In the output it doubles \\
.
import os
import time
import shutil
login = os.getlogin()
print(login)
while True:
filepath = input()
if filepath == 'end':
break
elif filepath[0:4] == 'copy': #im start command with 'copy' to copy file like 'copy C:\\Users...'
dirlist = os.listdir(filepath[5::])
print('Type dir to copied file')
codir = input('copy dir is..\n')
shutil.copy(filepath[5::], codir)
elif filepath[0:6] == 'search': #im start command with 'search' to copy file like 'search C:\\Users...'
print(filepath[6:-1])
dirlist = os.listdir(filepath[6::])
searchfile = input('file you want to search\n')
if searchfile in dirlist:
print("True")
else:
dirlist = os.listdir(filepath) # if it has no command, it's just show dir list
print(dirlist)
print('Script end!')
Output:
$ Tester.py
Artem
С:\\\Users
Traceback (most recent call last):
File "C:\Users\Artem\Desktop\Tester.py", line 24, in <module>
FileNotFoundError: [WinError 3] Системе не удается найти указанный путь: 'С:\\\\Users'
^System can't reach this way:'C:\\\\Users'
Why does it double \\
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
很久以前,微软选择使用反斜杠
\
作为分隔符,也许是出于天真(尽管这似乎是故意选择阻碍兼容性),而它一直是一个 转义字符表示永恒(表示特殊字符,例如\n
表示换行符或\t
表示选项卡),而其他理智世界则使用/
,原因有很多。\\
正在显示转义的反斜杠\
以实现兼容性系统无法显示此内容,但您可以使用
/
并且它会在以下环境中正常工作大多数(如果不是全部)情况您可能还会发现 pathlib 可以满足您所需的一切对于所有的路径,比纯粹的字符串处理更好!
Long ago, Microsoft chose to use the backslash
\
as a separator, perhaps through naivete (though it seems like an intentional choice to thwart compatibility), while it's been an escape character for an eternity (indicating a special character like\n
for a newline or\t
for a tab) and the rest of the sane world uses/
for this amongst many reasons.\\
is displaying an escaped backslash\
for compatibilityThe system has trouble displaying this, but you can use
/
and it'll work fine in most, if not all, casesYou may also find pathlib does everything you need for all your pathing more nicely than pure string handling!