为什么文件路径的输出中反斜杠加倍?

发布于 2025-01-13 15:31:07 字数 1337 浏览 1 评论 0原文

我对这个脚本有疑问。在输出中,它加倍 \\

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

愿与i 2025-01-20 15:31:07

很久以前,微软选择使用反斜杠 \ 作为分隔符,也许是出于天真(尽管这似乎是故意选择阻碍兼容性),而它一直是一个 转义字符表示永恒(表示特殊字符,例如 \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 compatibility

The system has trouble displaying this, but you can use / and it'll work fine in most, if not all, cases

You may also find pathlib does everything you need for all your pathing more nicely than pure string handling!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文