2017-08-07 86 views
0

我正在使用freesound API搜索和下载声音片段以用于训练神经网络。当脚本遇到名称中包含特殊字符的文件时,它将发出错误,脚本将停止。我想下载它带有特殊字符的文件并继续搜索。在名称中使用“/”或“”下载文件时出错

这里是我的代码:(API密钥是,只是把它现在)

import freesound, sys,os 


client = freesound.FreesoundClient() 
client.set_token("API KEY","token") 

results = client.text_search(query="dog", page_size=150, 
fields="id,name,previews") 



for sound in results: 
    if "/" or "\\" or '.' not in sound.name: 
     sound.retrieve_preview(".", sound.name+".mp3") 
     print(sound.name) 

错误代码:

FileNotFoundError: [Errno 2] No such file or directory: '.\\Dog eating Neck 
of Goose/Chewing/Breaking Bones.mp3' 
+0

'if“/”或“\\”或'。'不在sound.name'中:不会做你认为它的作用。这只是'真的' –

回答

0
if "/" or "\\" or '.' not in sound.name 

只是"/"(truthy)保存执行结果其他表达。由于它是“真实的”,它会在此处停止(短路),并且您始终输入if

尝试自然语言不是在Python中编码的正确方法。

除此之外,请注意,只有有问题的字符这里有斜杠/反斜杠(你应该测试冒号为好),但点是一个文件名OK

为了使其正常工作,我会用any这样:

if not any(x in "/\\:" for x in sound.name): 
+0

谢谢。这工作。 –

+0

好的,很高兴知道。告诉“它工作”的正确方法是接受答案:http://stackoverflow.com/help/someone-answers –

相关问题