我想我在 discord.py 机器人的逻辑中错误地添加了图像文件夹的路径
I think I'm adding the path to an image folder incorrectly in the logic of my discord.py bot
我正在尝试让机器人用 运行从我电脑上的文件夹中选择的图像进行响应:
if message.content == "look at this":
imgList = os.listdir("C:\Users\Alien\Desktop\BOTS\TAL\IMAGES")
imgString = random.choice(imgList)
path = "C:\Users\Alien\Desktop\BOTS\TAL\IMAGES" + imgString
await client.send_file(message.channel, path)
这是一个较长的 .py 文件的一部分,其中包含许多不同的代码,这些代码都可以与必要的 intros/outros 等一起正常工作
它 运行 在我添加这个之前很好但是现在当我尝试 运行 它打印:
C:\Users\Alien\PycharmProjects\tal-1.0\venv\Scripts\python.exe C:/Users/Alien/PycharmProjects/tal-1.0/tal-1.0.py
File "C:/Users/Alien/PycharmProjects/tal-1.0/tal-1.0.py", line 27
imgList = os.listdir("C:\Users\Alien\Desktop\BOTS\TAL\IMAGES")
^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape
Process finished with exit code 1
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape
这是告诉你转义字符错误,位置2-3是字符\U
\
是字符串的转义字符。它允许您在单引号字符串中包含单引号之类的内容:var = 'you\'re'
将保留单引号而不关闭字符串。
您在字符串中使用了转义字符 \
(您这样做是因为它是文件系统路径的一部分)。所以它试图解码下一个字符 U
,它不知道该怎么做,因为它不需要转义。
相反,您需要转义转义字符。您需要在每个 \
的地方写上 \
。
您的解决方案在所有路径中都需要这样的东西:
imgList = os.listdir("C:\Users\Alien\Desktop\BOTS\TAL\IMAGES")
我正在尝试让机器人用 运行从我电脑上的文件夹中选择的图像进行响应:
if message.content == "look at this":
imgList = os.listdir("C:\Users\Alien\Desktop\BOTS\TAL\IMAGES")
imgString = random.choice(imgList)
path = "C:\Users\Alien\Desktop\BOTS\TAL\IMAGES" + imgString
await client.send_file(message.channel, path)
这是一个较长的 .py 文件的一部分,其中包含许多不同的代码,这些代码都可以与必要的 intros/outros 等一起正常工作
它 运行 在我添加这个之前很好但是现在当我尝试 运行 它打印:
C:\Users\Alien\PycharmProjects\tal-1.0\venv\Scripts\python.exe C:/Users/Alien/PycharmProjects/tal-1.0/tal-1.0.py
File "C:/Users/Alien/PycharmProjects/tal-1.0/tal-1.0.py", line 27
imgList = os.listdir("C:\Users\Alien\Desktop\BOTS\TAL\IMAGES")
^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape
Process finished with exit code 1
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape
这是告诉你转义字符错误,位置2-3是字符\U
\
是字符串的转义字符。它允许您在单引号字符串中包含单引号之类的内容:var = 'you\'re'
将保留单引号而不关闭字符串。
您在字符串中使用了转义字符 \
(您这样做是因为它是文件系统路径的一部分)。所以它试图解码下一个字符 U
,它不知道该怎么做,因为它不需要转义。
相反,您需要转义转义字符。您需要在每个 \
的地方写上 \
。
您的解决方案在所有路径中都需要这样的东西:
imgList = os.listdir("C:\Users\Alien\Desktop\BOTS\TAL\IMAGES")