为什么我的文件路径中出现 Unicode 转义的 SyntaxError? [英] Why do I get a SyntaxError for a Unicode escape in my file path?

查看:18
本文介绍了为什么我的文件路径中出现 Unicode 转义的 SyntaxError?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要访问的文件夹名为 python,在我的桌面上.

当我尝试访问它时出现以下错误

<预><代码>>>>os.chdir('C:UsersexpoperialedDesktopPython')SyntaxError: (unicode error) 'unicodeescape' 编解码器无法解码位置 2-3 中的字节:截断的 UXXXXXXXX 转义

解决方案

您需要使用 raw 字符串、双斜杠或使用正斜杠:

r'C:UsersexpoperialedDesktopPython''C:\用户\expoperialed\桌面\Python''C:/Users/expoperialed/Desktop/Python'

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

您可以针对任何识别转义序列,如a or or x

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:UsersexpoperialedDesktopPython')
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated UXXXXXXXX escape

解决方案

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

r'C:UsersexpoperialedDesktopPython'
'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 or x, etc.

这篇关于为什么我的文件路径中出现 Unicode 转义的 SyntaxError?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆