【发布时间】:2014-05-13 19:33:31
【问题描述】:
我正在尝试检测带有扩展名列表的文件。
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
【问题讨论】:
-
file.endswith("")始终是True,所以你的elif可能没有t do what you think."。" not in file` 可能更接近。 -
是的,应该是一个文件夹。然后我必须进入这个文件夹来获取文件。 (我已将问题编辑得更明确)
-
您可以使用
os.path.splitext(filename)将扩展名从filename中取出:docs.python.org/2/library/os.path.html#os.path.splitext -
我发现了这个:elif os.path.isdir(file) == True: