Tutorial Example

Python Finds Files that Contain a Text String: A Beginner Guide – Python Tutorial

When we are reading other’s source codes, such as you are reading a wordpress theme codes,  we have to find a file which contains a text string or function from many source code files. It is a boring thing. Is there any easy way to find these files that contains a text string. The answer is yes.

In this tutorial, we will introduce how to find these files by python script.

1.Set a directory and traverse all files with a file extension

For example, we want to find all php files that contain a text string ‘acmephoto_action_feature_slider‘, first, we will list all php files in a directory.

import os
dir = r'E:\xampp\htdocs\www.pickdemo.com\wp-content\themes\acmephoto'
phpfiles = traverseDir(dir, file_type = '.php')

traverseDir() function is to list all files with a file extension. You can find this function in this tutorial.

Python Traverse Files in a Directory for Beginners – Python Tutorial

2.Read all files line by line and check whether contain a text string or not

After list all files in a directory, we will read these files line by line, then we check each line contains a text string(acmephoto_action_feature_slider) or not, if a line of a file contains, we will return it.

Here we create a function to check a file contains a text string or not

from unidecode import unidecode
def fileContain(file, text):
    with open(file, 'rb') as fin:  # read file
        for line in fin:
            content = line.decode("utf-8")
            content= unidecode(content)
            if text in content:
                return True
    return False

Then we will check all php files.

for f in phpfiles:
    flag = fileContain(f, text = 'acmephoto_action_feature_slider')  
    if flag:
        print(f)

Run this code, we will get result like:

Which means we find two files in this directory.