使用/和\\在python中指定文件夹位置有什么区别? [英] What is the difference between using / and \\ in specifying folder location in python?

查看:259
本文介绍了使用/和\\在python中指定文件夹位置有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Windows 10上使用的是python v3.6。在指定用于表示目录位置的字符串时,以下两种方法之间有什么区别?

I am using python v3.6 on Windows 10. When specifying a string to represent a directory location, what is the difference between the 2 approaches below?

folder_location = 'C:\\Users\\username\\Dropbox\\Inv'
folder_location = 'C:/Users/username/Dropbox/Inv'

这是我刚刚发布的另一个问题的后续问题。当我使用 \\ 而不是 / 时,我的问题得到解决。

This is a follow-up question to another question I just posted. My problem was solved when I used \\ instead of /.

硒硒Firefox配置文件将文件下载到自定义文件夹有什么问题?

推荐答案

在Unix系统上,该文件夹分隔符为 / ,而在Windows系统上,分隔符为 \ 。不幸的是,在大多数编程语言和基于文本的格式(包括C,Python和许多其他语言)中,此 \ 也是转义字符。奇怪的是,Windows路径中不允许使用 / 字符。

On Unix systems, the folder separator is /, while on Windows systems, the separator is \. Unfortunately this \ is also an escape character in most programming languages and text based formats (including C, Python and many others). Strangely enough a / character is not allowed in windows paths.

因此Windows上的Python旨在接受这两个<为了方便起见,在处理文件系统时,使用code> / 和 \ 作为文件夹分隔符。但是 \ 必须由另一个 \ 转义(除非您当然使用<$ c $之类的原始字符串c> r'反斜杠现在是普通字符\\\!')

So Python on windows is designed to accept both / and \ as folder separator when dealing with the filesystem, for convenience. But the \ must be escaped by another \ (unless of course you use raw strings like r'backslashes are now normal characters \\\ !')

硒将写入值Firefox首选项,与Python不同,Firefox首选项需要合适的分隔符。这就是为什么在您的示例中不使用正斜杠的原因。

Selenium, on the other hand, will write values into Firefox preferences, which, unlike Python, expects the appropriate kind of separator. That's why using forward slashes does not work in your example.

这篇关于使用/和\\在python中指定文件夹位置有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆