我期待通过他们的哈希自动检索我的世界的声音。为此,我开发了以下代码:
list=songname.split("/", -1)
last=int(len(list)-1)
songnamebut=list[last]
listmenos=list[:-1]
destination="/".join(listmenos)
destination = f"son/{destination}"
destination=os.path.abspath(destination)
destination=f"{destination}\\{songnamebut}"
#os.makedirs(os.path.dirname(songname), exist_ok=True)
shutil.copy2(source, destination)
else:
pass但我有一个错误,即:
Une exception s'est produite : FileNotFoundError
[Errno 2] No such file or directory: 'C:\\Users\\Sunday\\All MC 1.18 Sounds\\son\\minecraft\\sounds\\ambient\\cave\\cave1.ogg'
File "C:\Users\Sunday\All MC 1.18 Sounds\sounds_filter.py", line 28, in <module>
shutil.copy2(source, destination)我知道我的代码看起来很奇怪,例如,我创建了一个变量“songname但”,它只将声音的名称取为"sounds.ogg“,并将文件路径的其余部分分隔开(”我的音乐/声音/“),因为一开始,我认为它不能创建一个文件名相同的文件夹。
提前谢谢你!
正如我前面所说的,我首先尝试将文件夹名从“我的音乐/声音/声音. work”更改为“我的音乐/声音/”的路径,将"sound.ogg“更改为音频文件,但是它没有工作。我还试图预创建所有文件夹,方法是将shutil行作为注释,只留下os.makedirs。这起了作用,但没有解决问题。
发布于 2022-04-02 17:10:32
从先前的评论中扩展
问题是,在预期的结构中有一个额外的son文件夹是丢失的。因此python找不到目标文件夹并引发FileNotFoundError
.../All MC 1.18 Sounds/son/minecraft/...
vs
.../All MC 1.18 Sounds/minecraft/...
您还可以简化代码。
path, _, last = songname.rpartition("/")
destination = os.path.abspath(os.path.join("son", path))
os.makedirs(destination, exist_ok=True)
shutil.copy2(source, destination)或者在使用路径库时:
import pathlib
file_dir = pathlib.Path(songname).parent
destination = pathlib.Path("son") / file_dir
destination.mkdir(parents=True, exist_ok=True)
shutil.copy2(source, destination)https://stackoverflow.com/questions/71711903
复制相似问题