问题描述
我正在编写一个脚本,该脚本必须在Windows和Linux用户都手动修改过的目录上工作. Windows用户在分配文件名时往往根本不关心大小写.
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 case in assigning filenames.
有没有办法在Python的Linux端处理此问题,即我能否获得不区分大小写,类似glob的行为?
Is there a way to handle this on the Linux side in Python, i.e. can I get a case-insensitive, glob-like behaviour?
推荐答案
使用不区分大小写的正则表达式代替全局模式. fnmatch.translate
从全局模式生成正则表达式,所以
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)
为您提供了大小写不区分大小写的glob模式版本,作为已编译的RE.
gives you a case-insensitive version of a glob pattern as a compiled RE.
请记住,如果文件系统由类似Unix的文件系统上的Linux主机托管,则用户将能够在同一目录中创建文件foo
,Foo
和FOO
.
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.
这篇关于在Linux上忽略glob()中的大小写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!