Case-insensitive globbing

T

Thomas Philips

I'm using the function glob from module glob to obtain a list of all
files in a directory that match a pattern. Unfortunately, some
filenames are in upper case, others are in lower case and yet others
are in mixed case. How can I do a case-insenstive glob that picks up
all files that match a string regardless of case? If its any help, I'm
running Python 2.3.4 under Windows XP.

Thomas Philips
 
L

Larry Bates

Just use os.path.normcase() on all of them.

import glob
import os
flist=glob.glob(mask)
flist=[os.path.normcase(f) for f in flist]
....
<insert your code here>

HTH,
Larry Bates
Syscon, Inc.
 
E

Eddie Corns

I'm using the function glob from module glob to obtain a list of all
files in a directory that match a pattern. Unfortunately, some
filenames are in upper case, others are in lower case and yet others
are in mixed case. How can I do a case-insenstive glob that picks up
all files that match a string regardless of case? If its any help, I'm
running Python 2.3.4 under Windows XP.

If it works the same as it does on Unix then you can use the [Xx] construct
for each of the characters, eg *[Ff][Oo][Oo]* but I'll bet there are better
ways to do the task.

Eddie
 
D

Dennis Lee Bieber

are in mixed case. How can I do a case-insenstive glob that picks up
all files that match a string regardless of case? If its any help, I'm
running Python 2.3.4 under Windows XP.
Unless XP has made a drastic change in the handling of file
names, you don't worry about them...
['desktop.ini', 'Dennis Lee Bieber.asc', 'Doc1.doc', 'DF297.pdf']
['desktop.ini', 'Dennis Lee Bieber.asc', 'Doc1.doc', 'DF297.pdf']

Windows is a "case preserving, case insensitive" OS.

Well, actually I should modify that -- the Windows file explorer
tends to convert names in all "CAPS" into just first letter "Caps" when
displaying... But it is still all CAPS internally.

--
 
D

Derek Chen-Becker

Dennis said:
are in mixed case. How can I do a case-insenstive glob that picks up
all files that match a string regardless of case? If its any help, I'm
running Python 2.3.4 under Windows XP.

Unless XP has made a drastic change in the handling of file
names, you don't worry about them...


['desktop.ini', 'Dennis Lee Bieber.asc', 'Doc1.doc', 'DF297.pdf']

You can also make it cross platform by doing this:

flist = glob.glob("[dD]*.*")

Derek
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top