#!/usr/bin/env python3 from pathlib import Path def filter(paths, accept='*', reject=''): for p in paths: if p.match(accept) and not p.match(reject): yield p def filter2(paths, accept='*', reject=''): for p in paths: try: if p.match(reject): continue except ValueError: pass if p.match(accept): yield p print('This works because reject is set to a non-trival value:') for p in filter((Path(p) for p in ['bux', '.pux']), reject='.*'): print(' ', str(p)) print() print('This works with a more complicated version of filter:') for p in filter2((Path(p) for p in ['bux', '.pux'])): print(' ', str(p)) print() print('This works even though it should not because value of reject is invalid:') for p in filter2((Path(p) for p in ['bux', '.pux']), reject=None): print(' ', str(p)) print() print('This fails:') for p in filter((Path(p) for p in ['bux', '.pux'])): print(' ', str(p)) print()