Windows 上的奇怪路径分隔符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6928789/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Strange path separators on Windows
提问by Ian
I an running this code:
我正在运行此代码:
#!/usr/bin/python coding=utf8
# test.py = to demo fault
def loadFile(path):
f = open(path,'r')
text = f.read()
return text
if __name__ == '__main__':
path = 'D:\work\Kindle\srcs\test1.html'
document = loadFile(path)
print len(document)
It gives me a trackback
它给了我一个引用
D:\work\Kindle\Tests>python.exe test.py
Traceback (most recent call last):
File "test.py", line 11, in <module>
document = loadFile(path)
File "test.py", line 5, in loadFile
f = open(path,'r')
IOError: [Errno 22] invalid mode ('r') or filename: 'D:\work\Kindle\srcs\test1.html'
D:\work\Kindle\Tests>
If I change the path line to
如果我将路径线更改为
path = 'D:\work\Kindle\srcs\test1.html'
(note the double \\) it all works fine.
(注意双\\)一切正常。
Why? Either the separator is '\' or it is not, not a mix?
为什么?分隔符是 '\' 还是不是,不是混合?
System. Windows 7, 64bit, Python 2.7 (r27:82525, Jul 4 2010, 09:01:59) [MSC v.1500 32 bit (Intel)] on win32
系统。Windows 7,64 位,Python 2.7(r27:82525,2010 年 7 月 4 日,09:01:59)[MSC v.1500 32 位(英特尔)] 在 win32 上
Checked - and all the backslashes appear correctly.
已检查 - 所有反斜杠均正确显示。
回答by Skurmedel
You need to escape backslashes in paths with an extra backslash... like you've done for '\\test1.html'
.
您需要使用额外的反斜杠来转义路径中的反斜杠……就像您对'\\test1.html'
.
'\t'
is the escape sequence for a tab character.
'\t'
是制表符的转义序列。
'D:\work\Kindle\srcs\test1.html
is essentially 'D:\work\Kindle\srcs est1.html'
.
'D:\work\Kindle\srcs\test1.html
本质上是'D:\work\Kindle\srcs est1.html'
.
You could also use raw literals, r'\test1.html'
expands to:
您还可以使用原始文字,r'\test1.html'
扩展为:
'\test1.html'
回答by RichieHindle
Use raw strings for Windows paths:
对 Windows 路径使用原始字符串:
path = r'D:\work\Kindle\srcs\test1.html'
Otherwise the \t
piece of your string will be interpreted as a Tab character.
否则\t
,您的字符串部分将被解释为制表符。
回答by g.d.d.c
The backslash is an escape character when the next character combination would result in a special meaning. Take the following examples:
当下一个字符组合将产生特殊含义时,反斜杠是转义字符。以下列例子为例:
>>> '\r'
'\r'
>>> '\n'
'\n'
>>> '\b'
'\x08'
>>> '\c'
'\c'
>>>
r, n, and b all have special meanings when preceded by a backslash. The same is true for t, which would produce a tab. You either need to A. Double all your backslashes, for consistency, because '\\'
will produce a backslash, or, B, use raw strings: r'c:\path\to\my\file.txt'
. The preceding r will prompt the interpreter not to evaluate back slashes as escape sequences, preventing the \t
from appearing as a tab.
r、n 和 b 前面有反斜杠时都有特殊含义。t 也是如此,它会产生一个制表符。您可能需要A.双所有的反斜线,一致性,因为'\\'
会产生一个反斜杠,或B,使用原始的字符串:r'c:\path\to\my\file.txt'
。前面的 r 将提示解释器不要将反斜杠评估为转义序列,从而防止\t
显示为制表符。
回答by Ross B.
In addition to using a raw string (prefix string with the r character), the os.path module may be helpful to automatically provide OS-correct slashes when building a pathname.
除了使用原始字符串(带有 r 字符的前缀字符串)之外,os.path 模块可能有助于在构建路径名时自动提供操作系统正确的斜杠。
回答by thegrinner
The backslash \
is an escape characterin Python. So your actual filepath is going to be D:\work\Kindle\srcs<tab>est1.html
. Use os.sep, escape the backslashes with \\
or use a raw string by having r'some text'
.
反斜杠\
是Python 中的转义字符。所以你的实际文件路径将是D:\work\Kindle\srcs<tab>est1.html
. 使用 os.sep,使用 .sep 转义反斜杠\\
或使用原始字符串r'some text'
。
回答by bhadra
Gotcha — backslashes in Windows filenamesprovides an interesting overview.
问题 - Windows 文件名中的反斜杠提供了一个有趣的概述。