如何对给定后缀的文件执行不区分大小写的搜索?

How to perform a case-insensitive search for files of a given suffix?

我正在寻找 find $DIR -iname '*.mp3' 的等价物,我不想做古怪的 ['mp3', 'Mp3', MP3', etc] 事情。但我不知道如何将 re*.IGNORECASE 的东西与简单的 endswith() 方法结合起来。我的目标是不错过任何一个文件,我想最终将其扩展到其他 media/file types/suffixes.

import os
import re
suffix = ".mp3"

mp3_count = 0

for root, dirs, files in os.walk("/Volumes/audio"):
    for file in files:
        # if file.endswith(suffix):
        if re.findall('mp3', suffix, flags=re.IGNORECASE):
            mp3_count += 1

print(mp3_count)

任何反馈的TIA

你可以试试这个:)

import os
# import re
suffix = "mp3"

mp3_count = 0

for root, dirs, files in os.walk("/Volumes/audio"):
    for file in files:
        # if file.endswith(suffix):
        if file.split('.')[-1].lower() == suffix:
            mp3_count += 1

print(mp3_count)

Python的string.split()会将字符串分隔成一个列表,这取决于给定的参数,你可以通过[-1]访问后缀,最后一个元素名单

不用理会 os.walk。学会改用 the easier, awesome pathlib.Path。像这样:

from pathlib import Path

suffix = ".mp3"

mp3_count = 0

p = Path('Volumes')/'audio': # note the easy path creation syntax
# OR even:
p = Path()/'Volumes'/'audio': 

for subp in p.rglob('*'): #  recursively iterate all items matching the glob pattern
    # .suffix property refers to .ext extension
    ext = subp.suffix
    # use the .lower() method to get lowercase version of extension
    if ext.lower() == suffix: 
        mp3_count += 1

print(mp3_count)

"One-liner",如果你喜欢那种东西(为清楚起见多行):

sum(1 for subp in (Path('Volumes')/'audio').rglob('*')
     if subp.suffix.lower() == suffix)

.endswith 的正则表达式等价物是 $ 符号。

要使用上面的示例,您可以这样做;

re.findall('mp3$', suffix, flags=re.IGNORECASE):

尽管这样做可能更准确;

re.findall(r'\.mp3$', suffix, flags=re.IGNORECASE):

确保文件名以 .mp3 结尾,而不是选择 test.amp3.

等文件

这是一个很好的例子,说明了一种并不真正需要正则表达式的情况 - 因此,尽管欢迎您从这些示例中学习,但值得考虑其他回答者提供的替代方案。