python - How to check the file names are in os.listdir('.')? -


i use regex & string if file name & similars exists in os.listdir('.') or not, if exists print('yes'), if not print('no'), if file name doesn't exists in listdir('.') shows me yes.

how should check ?

search = str(args[0]) pattern = re.compile('.*%s.*\.pdf' %search, re.i) if filter(pattern.search, os.listdir('.')):     print('yes ...') else:     print('no ...') 

filter on python 3 lazy, doesn't return list, returns generator, "truthy", whether or not produce items (it doesn't know if until it's run out). if want check if got hits, efficient way try pull item it. on python 3, you'd use two-arg next lazily (so stop when hit , don't further):

if next(filter(pattern.search, os.listdir('.')), false): 

if need complete list la py2, you'd wrap in list constructor:

matches = list(filter(pattern.search, os.listdir('.'))) 

on python 2, existing code should work written.

i'll note, you're doing handled better the glob module; i'd recommend taking @ it.


Comments

Popular posts from this blog

how to add preprocess loader in webpack 2 -

python - django admin: changing the way a field (w/ relationship to another model) is submitted on a form so that it can be submitted multiple times -

sharepoint online - C# CSOM SPView ListItemCollection did not update after I add new field in the SP view -