Recursively expand and find the pattern of specific subdirectories
I'm looking for a way to search for specific subdirectories in python.
For example, a directory structure like this:
some_files/ common/ 2009/ 2010/ 2011/ ...
I only want to search in subdirectories starting with 2, so it should be something like "some_files / 2 *". I think this should be possible with glob.glob and os.walk (), but I cannot get it to work.
Now I am using:
files = [os.path.join(dirpath, f)
for dirpath, dirnames, files in os.walk(d)
for f in files if f.endswith(ext)]
but that doesn't fit your specific needs.
Can someone help me, would be greatly appreciated!
+3
source to share
2 answers
You can use glob with dirpath to find matching directories:
from glob import iglob
import os
files = []
ext = "py"
for dirpath, dirnames, file in os.walk(path):
match = next(iglob(os.path.join(dirpath, "2*")),"")
if match:
files.extend(iglob(os.path.join(match,"*.{}".format(ext))))
print(files)
Or, if you really want the comp list:
files = [f for dirpath, dirnames, file in os.walk(path) for f in
iglob(os.path.join(next(iglob(os.path.join(dirpath, "2*")),
'\\\\'), "*.{}".format(ext)))]
print(files)
+2
source to share
I would do it like this using pathlib (which is now part of the Python3 std lib):
from pathlib import Path
for subpath in Path().glob("2*):
for file in subpath.glob("*.ext"):
# ...
Update: pathlib is also available for Python 2.x (it has been ported and published on the Python Package Index ). Just:
$ pip install pathlib
+3
source to share