我知道如何解决这个问题(r、double \等等)。但它确实烧毁了我的大脑,我不能理解为什么下面的代码:
basepath = "C:\Users\Rio\Pictures\Screenshot"引发错误:
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape而下面的代码(在我看来是一样的)工作得很好:
basepath = "D:\Teaching content\Poker\Zenith Poker 2020"发布于 2020-08-27 23:24:49
当Python看到"\时,它会尝试进行unicode转义,这就是错误的根源。这取决于"\"后面的字符。在Windows上,文件路径通常包含"\U",这就去掉了Python。当您使用包含大量"\"字符的字符串时,请改用原始字符串。
basepath = r"C:\Users\Rio\Pictures\Screenshot"或者只是用双斜杠重写字符串
basepath = "C:\\Users\\Rio\\Pictures\\Screenshot"当Python看到"\\"时,它会将其理解为"\"。
发布于 2020-08-27 23:26:12
我认为你应该给PATH模块一个机会:
使用以下命令:
from pathlib import Path
basepath = Path('put your path here ') # using either r string or '\\'https://stackoverflow.com/questions/63619186
复制相似问题