python program to accept a string from the command line and print all files matching that string within a folder
Question: 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.


Answer: You can use this technique

    
    
    import os, fnmatch, sys
    def all_files(root, patterns='*', single_level=False, yield_folders=False):
        # Expand patterns from semicolon-separated string to list
        patterns = patterns.split(';')
        for path, subdirs, files in os.walk(root):
            if yield_folders:
                files.extend(subdirs)
            files.sort( )
            for name in files:
                for pattern in patterns:
                    if fnmatch.fnmatch(name, pattern):
                        yield os.path.join(path, name)
                        break
            if single_level:
                break
    
    user_definedpath, filepattern = sys.argv[1], sys.argv[2]
    # Invoking the all_files and putting them into list
    #thefiles = list(all_files('/tmp', '*.py;*.htm;*.html'))
    thefiles = list(all_files(user_definedpath, filepattern))
    print thefiles
    

Now you can save this file as `sample.py` in say `/tmp/abc/sample.py` Then you
can execute as `python /tmp/abc/sample.py "/tmp/xyz/" "*.py;*.txt"`

