Assuming that you are looking to match all .md
files that "look like" - defined here as containing the sub-string readme
(case insensitive) - from within the directory specified by my_dir
and all sub-directories contained within, the following should work for you.
import glob
import os
my_dir = 'my_dir' # IMPORTANT Please note that the leading '/' has been removed, this is to allow for desired behaviour in os.path.join() below
files = [ i for i in glob.glob(os.path.join(my_dir, '**', '*.md'), recursive=True) if 'readme' in os.path.basename(i.lower()) ]
if files:
# do stuff
It makes use of the builtin os
module to construct the path passed to glob.glob
that will search the top-level directory passed as the my_dir
variable recursively and return all paths ending with a .md
file.
The list of .md
file paths returned by the invocation of glob.glob
is filtered (by way of a list comprehension) to only include paths ending in .md
files containing the sub-string readme
(case insensitive).
The result is a list of all paths from within the my_dir
tree that end in an .md
file containing the case insensitive sub-string readme
within the filename portion of the path - or an empty list if none are found.