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

html - How to set bootstrap input responsive width? -

javascript - Highchart x and y axes data from json -

javascript - Get js console.log as python variable in QWebView pyqt -