Ignore case in glob() on Linux

前端 未结 6 798
星月不相逢
星月不相逢 2020-12-05 06:39

I\'m writing a script which will have to work on directories which are modified by hand by Windows and Linux users alike. The Windows users tend to not care at all about cas

6条回答
  •  青春惊慌失措
    2020-12-05 07:01

    Riffing off of @Timothy C. Quinn's answer, this modification allows the use of wildcards anywhere in the path. This is admittedly only case insensitive for the glob_pat argument.

    import re
    import os
    import fnmatch
    import glob
    
    def find_files(path: str, glob_pat: str, ignore_case: bool = False):
        rule = re.compile(fnmatch.translate(glob_pat), re.IGNORECASE) if ignore_case \
                else re.compile(fnmatch.translate(glob_pat))
        return [n for n in glob.glob(os.path.join(path, '*')) if rule.match(n)]
    

提交回复
热议问题