为什么反斜杠在文件路径的输出中加倍?
Why are backslashes doubled in output of file paths?
我对这个脚本有疑问。在输出中它加倍 \
.
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'
为什么会翻倍 \
?
很久以前,Microsoft 选择使用反斜杠 \
作为分隔符,这可能是出于天真(尽管这似乎是故意选择来阻碍兼容性),而它一直是 escape character永恒(表示一个特殊字符,如 \n
表示换行符或 \t
表示制表符),其他理智的世界出于多种原因使用 /
。
\
显示转义反斜杠 \
以实现兼容性
系统无法显示此内容,但您可以使用 /
,即使不是全部,也可以在大多数情况下正常工作
您可能还会发现 pathlib 比纯字符串处理更好地完成您所有路径所需的一切!
我对这个脚本有疑问。在输出中它加倍 \
.
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'
为什么会翻倍 \
?
很久以前,Microsoft 选择使用反斜杠 \
作为分隔符,这可能是出于天真(尽管这似乎是故意选择来阻碍兼容性),而它一直是 escape character永恒(表示一个特殊字符,如 \n
表示换行符或 \t
表示制表符),其他理智的世界出于多种原因使用 /
。
\
显示转义反斜杠 \
以实现兼容性
系统无法显示此内容,但您可以使用 /
,即使不是全部,也可以在大多数情况下正常工作
您可能还会发现 pathlib 比纯字符串处理更好地完成您所有路径所需的一切!