Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to search string in files recursively in python

I am trying to find all log files in my C:\ and then in these log file find a string. If the string is found the output should be the abs path of the log file where the string is found. below is what I have done till now.

import os
rootdir=('C:\\')
for folder,dirs,file in os.walk(rootdir):
    for files in file:
        if files.endswith('.log'):
            fullpath=open(os.path.join(folder,files),'r')
            for line in fullpath.read():
                if "saurabh" in line:
                    print(os.path.join(folder,files))
like image 935
user3050161 Avatar asked Aug 12 '15 11:08

user3050161


People also ask

How do you search for a string in a text file in Python?

Method 1: Finding the index of the string in the text file using readline() In this method, we are using the readline() function, and checking with the find() function, this method returns -1 if the value is not found and if found it returns 0.

How do I search for a recursive folder?

An easy way to do this is to use find | egrep string . If there are too many hits, then use the -type d flag for find. Run the command at the start of the directory tree you want to search, or you will have to supply the directory as an argument to find as well. Another way to do this is to use ls -laR | egrep ^d .

How do I search all files in a directory in Python?

To get a list of all the files and folders in a particular directory in the filesystem, use os. listdir() in legacy versions of Python or os. scandir() in Python 3.

How do I search in file Explorer using Python?

Python can search for file names in a specified path of the OS. This can be done using the module os with the walk() functions. This will take a specific path as input and generate a 3-tuple involving dirpath, dirnames, and filenames.


1 Answers

Your code is broken at:

for line in fullpath.read():

The statement fullpath.read() will return the entire file as one string, and when you iterate over it, you will be iterating a character at a time. You will never find the string 'saurabh' in a single character.

A file is its own iterator for lines, so just replace this statement with:

for line in fullpath:

Also, for cleanliness, you might want to close the file when you're done, either explicitly or by using a with statement.

Finally, you may want to break when you find a file, rather than printing the same file out multiple times (if there are multiple occurrences of your string):

import os
rootdir=('C:\\')
for folder, dirs, files in os.walk(rootdir):
    for file in files:
        if file.endswith('.log'):
            fullpath = os.path.join(folder, file)
            with open(fullpath, 'r') as f:
                for line in f:
                    if "saurabh" in line:
                        print(fullpath)
                        break
like image 65
Patrick Maupin Avatar answered Oct 09 '22 04:10

Patrick Maupin