Apr-24-2017, 03:50 AM
No
After PEP-471
It's not necessary to make a list,then loop over that list.
So can write it like this:
glob
is not needed,and glob is the wrong tool for this task it dos not recursively search sub folders as os.walk()
dos.After PEP-471
os.walk()
has got faster Python 3.5 or newer.Quote:In practice, removing all those extra system calls makes os.walk() about 8-9 times as fast on Windows ,
and about 2-3 times as fast on POSIX systems . So we're not talking about micro- optimizations.
Quote:Still learning, so please comment if there are better or more "pythonic" ways to do this.It's an okay solution,the list comprehensions can be a little long so get harder to read with when also put in
map lambda
.It's not necessary to make a list,then loop over that list.
So can write it like this:
import os cur_dir = os.path.dirname(os.path.realpath(__file__)) for root,dirs,files in os.walk(cur_dir): if any(f for f in files if f.endswith(('.jpg', '.png'))): print(root)
endswith(()
can also take a tuple as argument,so can search from more file extensions.