Python递归地查找具有特定扩展名的文件

2024-04-23 08:32:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试查找目录中的所有电影文件。修改了some code found on SO,但它只找到12个电影文件,而实际上目录中总共有17个.mp4s和.movs w/。。。最终,我尝试在设定的时间间隔内拍摄每个视频文件的截图,并在获得高清视频时快速生成“联系人表”。你知道吗

import os
import pandas as pd

folder_to_search = 'C:\\Users\\uname\\Desktop\\footage-directory'

extensions = ('.avi', '.mkv', '.wmv', '.mp4', '.mpg', '.mpeg', '.mov', '.m4v')


def findExt(folder):
    matches = []
    return [os.path.join(r, fn)
        for r, ds, fs in os.walk(folder) 
        for fn in fs if fn.endswith(extensions)]

print(len(findExt(folder_to_search)))
>>returns 12

enter image description here


Tags: 文件toinimport目录forsearch电影
1条回答
网友
1楼 · 发布于 2024-04-23 08:32:36
>>> 'venom_trailer.Mp4'.endswith('mp4') # <  file having .Mp4 extension is still a valid video file so it should have been counted. 
False

>>> 'venom_trailer.Mp4'.lower().endswith('mp4')
True

#      
>>> file_name = 'venom_trailer.Mp4'
>>> last_occurance_of_period = file_name.rfind('.')
>>> file_extension = file_name[last_occurance_of_period:]
>>> file_extension
'.Mp4'
>>> file_extension.lower() == '.mp4'
True
#      

# replace this line 
for fn in fs if fn.endswith(extensions) 
# with this
for fn in fs if fn.lower().endswith(extensions) 
# or with this
for fn in fs if fn[fn.rfind('.'):].lower() in extensions]

相关问题 更多 >