【问题标题】:.endswith missing file extention in a loop.endswith 在循环中缺少文件扩展名
【发布时间】:2013-06-23 10:37:42
【问题描述】:

我正在像这样从配置文件中加载扩展;

search_ext = tuple((config.get("Miscellaneous", "media") + config.get("Miscellaneous", "meta") + config.get("Miscellaneous", "other")).split('|'))

在配置文件中,扩展名是这样列出的;

媒体 = .mkv|.avi|.divx|.xvid|.mov|.wmv|.mp4|.mpg|.mpeg|.vob|.iso

meta = .nfo|.sub|.srt|.jpg|.jpeg|.gif|.txt

其他 = .exe|.pdf

然后使用 os.walk 遍历文件列表,使用 .endswith 进行搜索

if fileName.endswith(search_ext):

但它似乎总是错过最后一个扩展名,例如在这种情况下它将是.pdf。通过添加 .unknown 之类的虚假扩展名可以轻松绕过。但这是为什么呢?跟我的元组有关系吗?

【问题讨论】:

    标签: python


    【解决方案1】:

    在配置值周围去掉空格。 (这可能不是问题,根据config

    media 的最后一个元素和meta 的第一个元素连接在一起,没有分隔符。 (meta,other 也一样)

    search_ext = (
         config.get("Miscellaneous", "media").strip() + '|' +
         config.get("Miscellaneous", "meta").strip() + '|' +
         config.get("Miscellaneous", "other").strip()
    ).split('|')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-10
      • 2020-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-20
      相关资源
      最近更新 更多