Ignore case in glob() on Linux

前端 未结 6 791
星月不相逢
星月不相逢 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 06:55

    Here is my non-recursive file search for Python with glob like behavior for Python 3.5+

    # Eg: find_files('~/Downloads', '*.Xls', ignore_case=True)
    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 os.listdir(os.path.expanduser(path)) if rule.match(n)]
    

    Note: This version handles home directory expansion

提交回复
热议问题