为什么在文件路径中出现Unicode转义的SyntaxError?

问题:为什么在文件路径中出现Unicode转义的SyntaxError?

我要访问的文件夹称为python,位于我的桌面上。

尝试获取以下错误

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

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

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

回答 0

您需要使用原始字符串,将斜杠加倍或使用正斜杠:

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

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

对于任何已识别的转义序列,例如\at\x,您可以遇到许多其他问题。

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.

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


回答 1

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

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


回答 2

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

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

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

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:

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

For Python 2, just using “\” would work.


回答 3

f = open('C:\\Users\\Pooja\\Desktop\\trolldata.csv')

对于Python 3及更高版本的Python程序,请使用“ \\”。错误将得到解决。

f = open('C:\\Users\\Pooja\\Desktop\\trolldata.csv')

Use ‘\\’ for python program in Python version 3 and above.. Error will be resolved..


回答 4

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

另一种方法是先写

path = r’C:\ user \ ……….’(无论您使用什么路径)

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

All the three syntax work very well.

Another way is to first write

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

and then passing it to os.chdir(path)


回答 5

用这个

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

Use this

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

回答 6

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

在您的情况下,如果必须访问“ python”文件夹,则可能重新安装python,然后将安装路径更改为“ c:\ python”。否则,只需避免路径中出现“ … \ User …”,然后将项目放在C:下。

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.

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:.