【问题标题】:Error when downloading file with "/" or "\" in name下载名称中带有“/”或“\”的文件时出错
【发布时间】:2017-08-07 21:55:06
【问题描述】:

我正在使用 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'

【问题讨论】:

  • if "/" or "\\" or '.' not in sound.name: 没有做你认为的事情。只是True

标签: python api search


【解决方案1】:
if "/" or "\\" or '.' not in sound.name

只是"/"(真实)与其他表达方式相结合。因为它是“真实的”,所以它停在这里(短路),你总是输入if。

尝试自然语言并不是在 python 中编码的正确方式。

另外,请注意这里唯一有问题的字符是斜杠/反斜杠(您也应该测试冒号),但文件名可以使用点

为了让它正常工作,我会像这样使用any:

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

【讨论】:

猜你喜欢
  • 2019-02-24
  • 1970-01-01
  • 2011-09-25
  • 2015-06-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-15
相关资源
最近更新 更多