Ignore case in glob() on Linux

前端 未结 6 793
星月不相逢
星月不相逢 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:58

    Use case-insensitive regexes instead of glob patterns. fnmatch.translate generates a regex from a glob pattern, so

    re.compile(fnmatch.translate(pattern), re.IGNORECASE)
    

    gives you a case-insensitive version of a glob pattern as a compiled RE.

    Keep in mind that, if the filesystem is hosted by a Linux box on a Unix-like filesystem, users will be able to create files foo, Foo and FOO in the same directory.

提交回复
热议问题