我正在处理的代码抛出错误 Unsupported operand type(s) for +: 'WindowsPath' and 'str'
.我尝试了很多东西,但没有一个能解决这个问题(除了删除带有错误的行,但这没有帮助)。
对于上下文,此脚本(完成后)应该:
以便获取这些文件的程序播放新歌曲而不是旧歌曲,但可以随时恢复到原始歌曲。 (歌曲是从newgrounds下载并通过他们的newgrounds音频门户ID保存的)
我正在使用 Python 3.6.5
import os
import pathlib
from pathlib import Path
nspt = open ("NewSongsPath.txt", "rt")
nsp = Path (nspt.read())
spt = open("SongsPath.txt", "rt")
sp = (Path(spt.read()))
print("type the song ID:")
ID = input()
csp = str(path sp + "/" + ID + ".mp3") # this is the line throwing the error.
sr = open(csp , "rb")
sw = open(csp, "wb")
print (sr.read())
最佳答案
发生的事情是您使用“+”字符连接两种不同类型的数据
而不是使用错误行:
csp = str(path sp + "/" + ID + ".mp3")
尝试以这种方式使用它:
csp = str(Path(sp))
fullpath = csp + "/" + ID + ".mp3"
使用“fullpath”变量打开文件。
关于python - + : 'WindowsPath' and 'str' 不支持的操作数类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59870637/