我应该如何在 Python 字符串中编写 Windows 路径?
假设我需要引用路径C:\meshes\as
。如果我尝试直接编写它,例如 "C:\meshes\as"
,我会遇到问题 - 要么出现异常,要么路径不起作用。这是因为 \
充当转义字符吗?路径应该怎么写呢?
Suppose I need to refer to the path C:\meshes\as
. If I try writing that directly, like "C:\meshes\as"
, I encounter problems - either some exception, or the path just doesn't work. Is this because \
is acting as an escape character? How should I write the paths?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以使用always:
这在 Linux 和 Windows 中都有效。
另一种可能性是:
如果您对某些名称有问题,您也可以尝试原始字符串文字:
但是,最佳实践是使用
os.path
模块函数始终与操作系统的正确路径分隔符 (os.path.sep
) 连接:从 python 3.4 开始,您还可以使用
pathlib
模块。这相当于上面的:或者:
You can use always:
This works both in Linux and Windows.
Another possibility is:
If you have problems with some names you can also try raw string literals:
However, the best practice is to use the
os.path
module functions that always joins with the correct path separator (os.path.sep
) for your OS:From python 3.4 you can also use the
pathlib
module. This is equivalent to the above:or:
使用 os.path 模块。
或者使用原始字符串,
我也建议路径或文件名中不要有空格。您可以在字符串中使用双反斜杠。
Use the
os.path
module.Or use raw strings
I would also recommend no spaces in the path or file names. And you could use double backslashes in your strings.
是的,Python 字符串文字中的
\
表示转义序列的开始。在您的路径中,您有一个有效的两个字符转义序列\a
,它会折叠成 一个 字符,即 ASCII Bell:其他常见转义序列包括
\t
(制表符)、\n
(换行符) ,\r
(回车return):如您所见,在所有这些示例中,反斜杠和文字中的下一个字符都组合在一起,形成最终字符串中的单个字符。 Python 转义序列的完整列表位于此处 。
有多种方法可以解决这个问题:
Python 不会处理以
r< 为前缀的字符串文字中的转义序列/code> 或
R
:
<前><代码>>>> r'C:\网格\as'
'C:\\网格\\为'
>>>>>打印(r'C:\网格\ as')
C:\网格\as
Windows 上的 Python 也应该处理正斜杠。
您可以使用
os.path。加入
...<前><代码>>>>导入操作系统
>>>>> os.path.join('C:', os.sep, '网格', 'as')
'C:\\网格\\为'
... 或较新的
pathlib
模块<前><代码>>>>从 pathlib 导入路径
>>>>>路径('C:','/','网格','as')
WindowsPath('C:/mesh/as')
Yes,
\
in Python string literals denotes the start of an escape sequence. In your path you have a valid two-character escape sequence\a
, which is collapsed into one character that is ASCII Bell:Other common escape sequences include
\t
(tab),\n
(line feed),\r
(carriage return):As you can see, in all these examples the backslash and the next character in the literal were grouped together to form a single character in the final string. The full list of Python's escape sequences is here.
There are a variety of ways to deal with that:
Python will not process escape sequences in string literals prefixed with
r
orR
:Python on Windows should handle forward slashes, too.
You could use
os.path.join
...... or the newer
pathlib
module使用
Path
:Path
采用类似路径的字符串并调整当前操作系统(Windows 或 Linux)的所有内容。例如,在 Linux 上,它会将所有反斜杠转换为正斜杠,而在 Windows 上,它会执行相反的操作。完整文章:Python 3 快速提示:在 Windows、Mac 和 Linux 上处理文件路径的简单方法
我的经验:
os.path.join(...)
,然后切换到normpath(...)
最后切换到Path(...)
。在使用了这三个之后,Path 是世界上最好的。Path 相对于
os.path.join(... )
:/
连接两个不同的路径(见上文)。Path 相对于
normpath(...)
:/
连接路径,而不必回退到os.path.join(...)
,并使用嵌套的normpath调用来修复问题。Use
Path
:Path
takes a path-like string and adjusts everything for the current OS, either Windows or Linux. For example, on Linux it would convert all backslashes to forward slashes, and on Windows it would do the reverse.Full article: Python 3 Quick Tip: The easy way to deal with file paths on Windows, Mac and Linux
My experience:
os.path.join(...)
, then switched tonormpath(...)
then finally switched toPath(...)
. Having used all three, Path is the best of all worlds.Advantages of Path over
os.path.join(...)
:/
(see above).Advantages of Path over
normpath(...)
:/
rather than having to fall back toos.path.join(...)
, with nested normpath calls to fix things up.Python 原始字符串是通过在字符串文字前添加“r”或“R”前缀来创建的。 Python 原始字符串将反斜杠 () 视为文字字符。当我们想要一个包含反斜杠的字符串并且不希望它被视为转义字符时,这非常有用。
手动执行,例如:
或使用 r 或 R:
Python raw string is created by prefixing a string literal with ‘r’ or ‘R’. Python raw string treats backslash () as a literal character. This is useful when we want to have a string that contains backslash and don’t want it to be treated as an escape character.
Doing Manually Such as:
or by using r or R: