Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

find all files containing a string in python with glob module

import glob
# path of the current directory
path = './'
text_files = glob.glob(path + '**/*.txt',recursive=True)
print(text_files)


for text_file in text_files:
    required_str= 'Python'
    try:
        with open(text_file) as f:
            # read the file as a string
            text_data = f.read()
            # if the string is find
            if(required_str in text_data):
                print(text_file)
    except:
        pass
Source by www.alixaprodev.com #
 
PREVIOUS NEXT
Tagged: #find #files #string #python #glob #module
ADD COMMENT
Topic
Name
4+8 =