def find_files(directory, pattern, recursive=True):
if not os.path.isdir(directory):<EOL><INDENT>if os.path.exists(directory):<EOL><INDENT>raise IOError(directory + '<STR_LIT>')<EOL><DEDENT>else:<EOL><INDENT>raise IOError(directory + "<STR_LIT>")<EOL><DEDENT><DEDENT>if recursive:<EOL><INDENT>for root, _, files in os.walk(directory):<EOL><INDENT>for basename in files:<EOL><INDENT>if fnmatch.fnmatch(basename, pattern):<EOL><INDENT>filename = os.path.join(root, basename)<EOL>yield filename<EOL><DEDENT><DEDENT><DEDENT><DEDENT>else:<EOL><INDENT>root = directory<EOL>for basename in os.listdir(root):<EOL><INDENT>if fnmatch.fnmatch(basename, pattern):<EOL><INDENT>filename = os.path.join(root, basename)<EOL>if os.path.isfile(filename):<EOL><INDENT>yield filename<EOL><DEDENT><DEDENT><DEDENT><DEDENT>
Find files by pattern in directory