File paths in python, possible writing styles

时间:2018-09-19 08:18:49

标签: python string path backslash

I know that the following string definitions represent (python internal) the same (basic knowledge):

s1 = "D:\\users\\xy\\Desktop\\PC_daten.txt"
s2 = r"D:\users\xy\Desktop\PC_daten.txt"

Therefore if you make it e.g. interactive in PythonWin:

>>> s1 = "D:\\users\\xy\\Desktop\\PC_daten.txt"
>>> s1
'D:\\users\\xy\\Desktop\\PC_daten.txt'
>>> print s1
D:\users\xy\Desktop\PC_daten.txt
>>> s2 = r"D:\users\xy\Desktop\PC_daten.txt"
>>> s2
'D:\\users\\xy\\Desktop\\PC_daten.txt'
>>> print s2
D:\users\xy\Desktop\PC_daten.txt

Clear for me (understand)

But what me confuse is that at the python built in function open all this (partly crazy) path-codings work (interactive tested in PythonWin):

>>> a = open("D:\users\xy\Desktop\PC_daten.txt")
>>> a
<open file 'D:\\users\\xy\\Desktop\\PC_daten.txt', mode 'r' at 0x00000000046989C0>
>>> b = open("D:\\users\\xy\\Desktop\\PC_daten.txt")
>>> b
<open file 'D:\\users\\xy\\Desktop\\PC_daten.txt', mode 'r' at 0x00000000048B2300>
>>> c = open(r"D:\users\xy\Desktop\PC_daten.txt")
>>> c
<open file 'D:\\users\\xy\\Desktop\\PC_daten.txt', mode 'r' at 0x00000000048B20C0>
>>> d = open(r"D:\\users\\xy\\Desktop\\PC_daten.txt")
>>> d
<open file 'D:\\\\users\\\\xy\\\\Desktop\\\\PC_daten.txt', mode 'r' at 0x00000000048B2390>

My questions:

  • Where is this behaviour documented?
  • Have anybody background-informations?

Tests was made at Windows 7 OS and with PythonWin 2.7.13

2 个答案:

答案 0 :(得分:0)

For details, check section 2.4.1 in the docs

Let's take a look at the different strings and why they are working:

>>> a = open("D:\users\xy\Desktop\PC_daten.txt")
>>> a
<open file 'D:\\users\\xy\\Desktop\\PC_daten.txt', mode 'r' at 0x00000000046989C0>

The \ is the escape character in a string, which is used for special characters like a newline (\n). In this case you are lucky that none of the combinations \u \x \P indicate a special character like that, hence it works, as opposed to open("foo\newfile.txt")

>>> b = open("D:\\users\\xy\\Desktop\\PC_daten.txt")
>>> b
<open file 'D:\\users\\xy\\Desktop\\PC_daten.txt', mode 'r' at 0x00000000048B2300>

Save method, the \\ constructs escapes the \, hence you would also be able to do open("foo\\newline")

>>> c = open(r"D:\users\xy\Desktop\PC_daten.txt")
>>> c
<open file 'D:\\users\\xy\\Desktop\\PC_daten.txt', mode 'r' at 0x00000000048B20C0>

Also save, since the prefix rindicates a raw string, the \ are taken literally and not as escape sequences

>>> d = open(r"D:\\users\\xy\\Desktop\\PC_daten.txt")
>>> d
<open file 'D:\\\\users\\\\xy\\\\Desktop\\\\PC_daten.txt', mode 'r' at 0x00000000048B2390>

Here you have a raw string (preceeded with r) but still use double \, hence you have \\ in the path. This works because windows understand paths with double \ which you can also confirm in the cmd

答案 1 :(得分:0)

此字符串行为记录在(Python v3.7 docs)2.4.1. String and Bytes literals下,open()的行为记录在2. Built-in Functions下。
具体来说,对于open()中的文件/路径,如16.1.2. Process Parameters中的澄清,在条目class os.PathLike中。诚然,要真正理解此参数,需要阅读很多内容。

总而言之:r“字符串”是原始字符串。使用原始斜杠时,您无需转义诸如\\之类的斜杠即可产生单个斜杠\

在第二个块中,为了清楚起见,您正在查看变量本身,并通过它们的字符串表示形式(当使用print时)查看了它们(我想您提到的您很好理解,但是我只想重复一遍)返回给您确定)。

展望未来,在处理文件和文件夹时,python版本将全部使用PathLike对象。我猜您仍在使用Python 2.x,因为在运行print s1时没有收到错误-在Python 3.x中应该是print(s1)。我建议尽快迁移到Python 3,这确实更好。

我希望能成功回答您的问题吗?