function that select ALL paths in folder containing specificic pieces of string
Question: I would like to use the os.walk method in Python in order to select ALL the
files that contain certain strings in their name. Here the code I wrote

    
    
    def func(root = root, element = ''):
        c = []
        for path, subdirs, files in os.walk(root):
            c = c + [ os.path.join(path, name) for name in files \
            if element in os.path.join(path, name) ]
        return c
    

Now if I type

    
    
    func(root, 'Raman')
    

than I select all the files which contain the string 'Raman' in their name. I
would like to have a function in which the second argument is a list of string

    
    
    func(root, ['string 1', 'string 2', ... 'string n'])
    

and it select ALL the paths which contain 'string 1', 'string 2' .. 'string
n', but the problem is harder than it looks like. Is there anyone who could
suggest me a modification of the previous code?


Answer: You may use `any` built-in function:

    
    
    import os
    
    
    def func(root, elements):
        c = []
        for path, subdirs, files in os.walk(root):
            c = c + [os.path.join(path, name) for name in files \
                     if any(element in os.path.join(path, name) for element in elements)]
        return c
    

Unfortunately, `func` in current form does not read that well. I'd suggest
converting it to generator

    
    
    def func(root, elements):
        for root_path, _, files in os.walk(root):
            for name in files:
                full_path = os.path.join(root_path, name)
                if any(element in full_path for element in elements):
                    yield full_path
    

