Python Forum
Simple script to find directories containing pictures
Thread Rating:
  • 3 Vote(s) - 3.33 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Simple script to find directories containing pictures
#4
No 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.
Reply


Messages In This Thread
RE: Simple script to find directories containing pictures - by snippsat - Apr-24-2017, 03:50 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Just a simple vshare.io api script Aqua22 5 4,357 May-11-2020, 04:34 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020