使用具有多个扩展名的endswith

编程入门 行业动态 更新时间:2024-10-25 22:24:20
本文介绍了使用具有多个扩展名的endswith的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试检测带有扩展名列表的文件.

I'm trying to detect files with a list of extensions.

ext = [".3g2", ".3gp", ".asf", ".asx", ".avi", ".flv", \ ".m2ts", ".mkv", ".mov", ".mp4", ".mpg", ".mpeg", \ ".rm", ".swf", ".vob", ".wmv"] if file.endswith(ext): # how to use the list ? command 1 elif file.endswith(""): # it should be a folder command 2 elif file.endswith(".other"): # not a video, not a folder command 3

推荐答案

为此使用元组.

>>> ext = [".3g2", ".3gp", ".asf", ".asx", ".avi", ".flv", \ ".m2ts", ".mkv", ".mov", ".mp4", ".mpg", ".mpeg", \ ".rm", ".swf", ".vob", ".wmv"] >>> ".wmv".endswith(tuple(ext)) True >>> ".rand".endswith(tuple(ext)) False

与其每次都进行转换,不如将其转换为元组一次.

Instead of converting everytime, just convert it to tuple once.

更多推荐

使用具有多个扩展名的endswith

本文发布于:2023-11-02 17:52:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1553029.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   扩展名   endswith

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!