我应该如何在python字符串字面上编写Windows路径?
假设我需要参考路径 c:\ meshes \ as
。如果我尝试直接编写,例如“ C:\ meshes \ as”
,我会遇到问题 - 某些异常,或者该路径不起作用。这是因为 \
是逃生角色吗?我应该如何写路径?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以始终使用:
这在Linux和Windows中都起作用。
另一种可能性是:
如果您有一些名称的问题,也可以尝试原始字符串文字:
但是,最好的做法是使用
从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
(tab),\ n
(line feed) ,\ r
(马车返回):如您所见,在所有这些示例中,backslash和文字中的下一个字符被分组在一起,以在最终字符串中形成单个字符。 Python的逃生序列的完整列表是在这里 。
有多种处理方法:
python不会在字符串文字中处理
r
r < /code>或
r
:
Windows上的python也应处理前向斜线。
您可以使用
os.path。加入
......或新的
pathlib
模块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使用
路径
:路径
采用类似路径的字符串,并调整当前操作系统的所有内容,无论是Windows还是Linux。例如,在Linux上,它将将所有后斜线转换为前向斜线,在窗口上,它将进行反向。全文: python 3快速提示:处理Windows,Mac和Linux上的文件路径的简便方法
我的经验:
OS.Path花了6个月的时间。加入(...)
,然后切换到normpath(...)
,然后最终切换到path(...)
。使用了这三个,道路是所有世界中最好的。:
/
(见上文)连接两个不同的路径。:
/
加入路径,而不是必须返回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 Raw String是通过将字符串字面的字符串与“ R”或“ R”结合在一起而创建的。 Python Raw String将Backslash()视为字面角色。当我们想要一个包含后斜切并且不希望将其视为逃生角色的字符串时,这很有用。
手动执行:
或使用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: