from subprocess import getoutput filenames = getoutput("dir").split() search_term = input("Enter the search term: ") for fn in filenames: # each filename is fn # Prints out the file and the number of lines inside it #print(fn, ": ", getoutput("cat " + fn + " | wc -l ")) if not ".py" in fn: continue try: file_object = open(fn) lines_from_file = file_object.readlines() except: print("Can't read file: ", fn) # Now look for matches in the file! for line in lines_from_file: if search_term in line: print(fn, ": ", line.strip())