Searching through more than one file.

Rick Johnson rantingrickjohnson at gmail.com
Mon Dec 29 02:47:23 EST 2014


On Sunday, December 28, 2014 11:29:48 AM UTC-6, Seymore4Head wrote:
> I need to search through a directory of text files for a string.
> Here is a short program I made in the past to search through a single
> text file for a line of text.

Step1: Search through a single file. 
# Just a few more brush strokes...

Step2: Search through all files in a directory. 
# Time to go exploring! 

Step3: Option to filter by file extension. 
# Waste not, want not!

Step4: Option for recursing down sub-directories. 
# Look out deeply nested structures, here i come!
# Look out deeply nested structures, here i come!
# Look out deeply nested structures, here i come!
# Look out deeply nested structures, here i come!
# Look out deeply nested structures, here i come!
# Look out deeply nested structures, here i come!
# Look out deeply nested structures, here i come!
 [Opps, fell into a recursive black hole!]
# Look out deeply nested structures, here i come!
# Look out deeply nested structures, here i come!
# Look out deeply nested structures, here i come!
# Look out deeply nested structures, here i come!
 [BREAK]
# Whew, no worries, MaximumRecursionError is my best friend! 

;-)

In addition to the other advice, you might want to check out os.walk().



More information about the Python-list mailing list