为什么在文件路径中为Unicode转义获取SyntaxError?

时间:2021-02-27 08:58:11

The folder I want to get to is called python and is on my desktop.

我想要的文件夹名为python,位于我的桌面上。

I get the following error when I try to get to it

当我试图达到它时,我收到以下错误

>>> os.chdir('C:\Users\expoperialed\Desktop\Python')
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape

7 个解决方案

#1


143  

You need to use a raw string, double your slashes or use forward slashes instead:

您需要使用原始字符串,双斜线或使用正斜杠:

r'C:\Users\expoperialed\Desktop\Python'
'C:\\Users\\expoperialed\\Desktop\\Python'
'C:/Users/expoperialed/Desktop/Python'

In regular python strings, the \U character combination signals a extended Unicode codepoint escape.

在常规python字符串中,\ U字符组合表示扩展的Unicode代码点转义。

You can hit any number of other issues, for any of the recognised escape sequences, such as \a or t or \x, etc.

对于任何已识别的转义序列,您可以遇到任意数量的其他问题,例如\ a或t或\ x等。

#2


9  

C:\\Users\\expoperialed\\Desktop\\Python This syntax worked for me.

C:\\ Users \\ expoperialed \\ Desktop \\ Python这个语法对我有用。

#3


7  

This usually happens in Python 3. One of the common reasons would be that while specifying your file path you need "\\" instead of "\". As in:

这通常发生在Python 3中。一个常见的原因是,在指定文件路径时,您需要“\\”而不是“\”。如:

filePath = "C:\\User\\Desktop\\myFile"

For Python 2, just using "\" would work.

对于Python 2,只需使用“\”即可。

#4


5  

f = open('C:\\Users\\Pooja\\Desktop\\trolldata.csv')... Use '\\' for python program in python version 3 and above.. Error will be resolved..

f = open('C:\\ Users \\ Pooja \\ Desktop \\ trolldata.csv')...在python版本3及更高版本的python程序中使用'\\'..错误将被解决..

#5


2  

All the three syntax work very well.

所有这三种语法都能很好地工作。

Another way is to first write

另一种方法是先写

path = r'C:\user\...................' (whatever is the path for you)

path = r'C:\ user \ ...................'(无论你走的是什么路径)

and then passing it to os.chdir(path)

然后将其传递给os.chdir(路径)

#6


1  

Use this

用这个

os.chdir('C:/Users\expoperialed\Desktop\Python')

#7


-1  

I had the same error. Basically, I suspect that the path cannot start either with "U" or "User" after "C:\". I changed my directory to "c:\file_name.png" by putting the file that I want to access from python right under the 'c:\' path.

我有同样的错误。基本上,我怀疑在“C:\”之后路径无法以“U”或“User”开头。通过将我想要从python访问的文件放在'c:\'路径下,我将目录更改为“c:\ file_name.png”。

In your case, if you have to access the "python" folder, perhaps reinstall the python, and change the installation path to something like "c:\python". Otherwise, just avoid the "...\User..." in your path, and put your project under C:.

在您的情况下,如果您必须访问“python”文件夹,可能重新安装python,并将安装路径更改为“c:\ python”之类的内容。否则,只需避免路径中的“... \ User ...”,并将项目置于C:。

#1


143  

You need to use a raw string, double your slashes or use forward slashes instead:

您需要使用原始字符串,双斜线或使用正斜杠:

r'C:\Users\expoperialed\Desktop\Python'
'C:\\Users\\expoperialed\\Desktop\\Python'
'C:/Users/expoperialed/Desktop/Python'

In regular python strings, the \U character combination signals a extended Unicode codepoint escape.

在常规python字符串中,\ U字符组合表示扩展的Unicode代码点转义。

You can hit any number of other issues, for any of the recognised escape sequences, such as \a or t or \x, etc.

对于任何已识别的转义序列,您可以遇到任意数量的其他问题,例如\ a或t或\ x等。

#2


9  

C:\\Users\\expoperialed\\Desktop\\Python This syntax worked for me.

C:\\ Users \\ expoperialed \\ Desktop \\ Python这个语法对我有用。

#3


7  

This usually happens in Python 3. One of the common reasons would be that while specifying your file path you need "\\" instead of "\". As in:

这通常发生在Python 3中。一个常见的原因是,在指定文件路径时,您需要“\\”而不是“\”。如:

filePath = "C:\\User\\Desktop\\myFile"

For Python 2, just using "\" would work.

对于Python 2,只需使用“\”即可。

#4


5  

f = open('C:\\Users\\Pooja\\Desktop\\trolldata.csv')... Use '\\' for python program in python version 3 and above.. Error will be resolved..

f = open('C:\\ Users \\ Pooja \\ Desktop \\ trolldata.csv')...在python版本3及更高版本的python程序中使用'\\'..错误将被解决..

#5


2  

All the three syntax work very well.

所有这三种语法都能很好地工作。

Another way is to first write

另一种方法是先写

path = r'C:\user\...................' (whatever is the path for you)

path = r'C:\ user \ ...................'(无论你走的是什么路径)

and then passing it to os.chdir(path)

然后将其传递给os.chdir(路径)

#6


1  

Use this

用这个

os.chdir('C:/Users\expoperialed\Desktop\Python')

#7


-1  

I had the same error. Basically, I suspect that the path cannot start either with "U" or "User" after "C:\". I changed my directory to "c:\file_name.png" by putting the file that I want to access from python right under the 'c:\' path.

我有同样的错误。基本上,我怀疑在“C:\”之后路径无法以“U”或“User”开头。通过将我想要从python访问的文件放在'c:\'路径下,我将目录更改为“c:\ file_name.png”。

In your case, if you have to access the "python" folder, perhaps reinstall the python, and change the installation path to something like "c:\python". Otherwise, just avoid the "...\User..." in your path, and put your project under C:.

在您的情况下,如果您必须访问“python”文件夹,可能重新安装python,并将安装路径更改为“c:\ python”之类的内容。否则,只需避免路径中的“... \ User ...”,并将项目置于C:。