Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Way to ignore case in iteration of glob

Tags:

python

Is there a simpler way to do the following to iterate over xls files regardless of capitalization?

for file in [glob.glob(os.path.join(dir, '*.xls')), glob.glob(os.path.join(dir, '*.XLS'))]:
like image 559
David542 Avatar asked Feb 07 '12 23:02

David542


2 Answers

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]')):
like image 164
phihag Avatar answered Oct 02 '22 20:10

phihag


You could do the matching "manually" without glob:

for file in os.listdir(dir):
    if not file.lower().endswith('.xls'):
        continue
    ...
like image 38
sth Avatar answered Oct 02 '22 22:10

sth