【发布时间】:2020-05-09 06:10:57
【问题描述】:
我正在处理的代码引发错误Unsupported operand type(s) for +: 'WindowsPath' and 'str'。我已经尝试了很多东西,但都没有解决这个问题(除了删除有错误的行,但这没有帮助)。
就上下文而言,这个脚本(完成后)应该:
- 根据您输入的 ID(在 SongsPath.txt 中指定的目录中)查找文件 (mp3)
- 备份它
- 然后将其替换为另一个文件(重命名为上一个文件的名称)
以便获取这些文件的程序播放新歌曲而不是旧歌曲,但可以随时恢复到原始歌曲。 (歌曲从 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())
【问题讨论】:
-
Path()不返回字符串,而是返回Path对象。 -
始终共享整个错误消息。你从那个错误中理解了什么?我建议使用上下文管理器来处理文件对象。
-
path sp是复制错误吗?应该只是sp吗?
标签: python file-handling operands