Program python to accept a string from the command line and print all files corresponding to that string in a folder

advertisements

How do i write a program to accept a string from the command line and print all filenames matching that string within a folder(also subfolders)?

I'm looking for a pattern match.


    import sys, os, operator, re
    def filesMatching(pattern, top):
        return [f for f in os.listdir(top) if os.path.isfile(os.path.join(top,f)) and re.match(pattern, f)] + reduce (operator.__concat__, [filesMatching (pattern, os.path.join(top,f)) for f in os.listdir (top) if os.path.isdir(os.path.join(top,f))], [])

    print (filesMatching (sys.argv[1], sys.argv[2]))