Python Jupyter笔记本在一个实例中抛出unicode错误,但在另一个实例却没有



我知道如何解决这个问题(r、double\等(,但它确实让我伤透了脑筋,我不明白为什么会出现以下代码:

basepath = "C:UsersRioPicturesScreenshot"

引发错误:

SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated UXXXXXXXX escape

而下面的代码(看起来和我一样(运行得很好:

basepath = "D:Teaching contentPokerZenith Poker 2020"

当Python看到"时,它试图执行unicode转义,这是错误的来源。它取决于""后面的字符。在Windows上,文件路径通常包含"U",这会抛出Python。当您使用包含大量""字符的字符串时,请改用原始字符串。

basepath = r"C:UsersRioPicturesScreenshot"

或者用双斜线重写字符串

basepath = "C:\Users\Rio\Pictures\Screenshot"

当Python看到"\"时,它将其理解为""

我认为您应该尝试一下PATH模块:

使用这个:

from pathlib import Path
basepath = Path('put your path here ') # using either r string or '\'

最新更新