Ignore case in glob() on Linux
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.
Case-insensitive Glob on zsh/bash
ZSH:
$ unsetopt CASE_GLOB
Or, if you don't want to enable case-insensitive globbing in general, you can activate it for only the varying part:
$ print -l (#i)(somelongstring)*
This will match any file that starts with "somelongstring" (in any combination of lower/upper case). The case-insensitive flag applies for everything between the parentheses and can be used multiple times. Read the manual zshexpn(1)
for more information.
UPDATE
Almost forgot, you have to enable extendend globbing for this to work:
setopt extendedglob
Way to ignore case in iteration of glob
glob and the underlying fnmatch have no flag for case-independence, but you can use brackets:
for file in glob.iglob(os.path.join(dir, '*.[xX][lL][sS]')):
Ignore case when matching file name extensions with find
GNU
versions of find
have an -iname
flag which enables case insensitive matching of file name globs,
find ./ -iname '*.jpg'
or if you are on a system without GNU
utilities, use the bracket expressions to the glob
find ./ -name '*.[Jj][Pp][Gg]'
If you are interested in multiple name filters, just use the -o
expression for including multiple name globs
find ./ \( -iname "*.jpg" -o -iname "*.jpeg" \)
PHP - Case insensitive File Search
I ultimately ended up fetching all images from the folder and checking for each sku in the image name array.
The following code solved my problem:
$path = $image_path ."/*.{jpg,png,gif}";
$all_images = glob($path, GLOB_BRACE);
$icount = count($all_images);
for($i = 0; $i < $icount; $i++)
{
$all_images[$i] = str_replace($image_path.'/', '', $all_images[$i]);
}
foreach($products as $product){
$matches = preg_grep ('/^'.$product['sku'].'(\w+)/i', $all_images);
}
Nevertheless, I would love to see case-insensitive glob
implemented in future.
Related Topics
Find Nearest Value in Numpy Array
How to Prompt For User Input and Read Command-Line Arguments
On Linux Suse or Redhat, How to Load Python 2.7
Python: Interplay Between Lib/Site-Packages/Site.Py and Lib/Site.Py
Pyserial Works Fine in Python Interpreter, But Not Standalone
How to Programmatically Edit Excel Sheets
Capturing Output of Python Script Run Inside a Docker Container
How to Use Os.Umask() in Python
How to Get the Python Call Stack with the Linux Perf
How Can Python Handle Systemctl Stop
Is There a Simple Way to Remove Multiple Spaces in a String
Pandas Read_SQL With Parameters
Standalone Python Applications in Linux
Permission Denied When Executing Python File in Linux
Cannot "Pip Install Cryptography" in Docker Alpine Linux 3.3 with Openssl 1.0.2G and Python 2.7