Filter file list in python/ lowercase and uppercase extension files -


i filtering file list using line:

mylist = filter(lambda x: x.endswith(('.doc','.txt','.dat')), os.listdir(path))

the line above filter lowercase extension files. therefore, there elegant way make filter uppercase extension files?

you need add .lower() lambda function

mylist = filter(lambda x: x.lower().endswith(('.doc','.txt','.dat')), os.listdir(path)) 

i'd prefer use os.path.splitext list comprehension

from os.path import splitext my_list = [x x in os.listdir(path) if splitext(x)[1].lower() in {'.doc', '.txt', '.dat'}] 

still bit single line, perhaps

from os.path import splitext  def valid_extension(x, valid={'.doc', '.txt', '.dat'}):     return splitext(x)[1].lower() in valid  my_list = [x x in os.listdir(path) if valid_extension(x)] 

Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

c# - must be a non-abstract type with a public parameterless constructor in redis -

ajax - PHP/JSON Login script (Twitter style) not setting sessions -