Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find files in a directory containing desired string in Python

Tags:

python

I'm trying to find a string in files contained within a directory. I have a string like banana that I know that exists in a few of the files.

import os
import sys

user_input = input("What is the name of you directory?")
directory = os.listdir(user_input)
searchString = input("What word are you trying to find?")

for fname in directory: # change directory as needed
    if searchString in fname:
        f = open(fname,'r')
        print('found string in file %s') %fname
    else:
        print('string not found')

When the program runs, it just outputs string not found for every file. There are three files that contain the word banana, so the program isn't working as it should. Why isn't it finding the string in the files?

like image 599
rigning Avatar asked Dec 30 '15 13:12

rigning


2 Answers

You are trying to search for string in filename, use open(filename, 'r').read():

import os

user_input = input('What is the name of your directory')
directory = os.listdir(user_input)

searchstring = input('What word are you trying to find?')

for fname in directory:
    if os.path.isfile(user_input + os.sep + fname):
        # Full path
        f = open(user_input + os.sep + fname, 'r')

        if searchstring in f.read():
            print('found string in file %s' % fname)
        else:
            print('string not found')
        f.close()

We use user_input + os.sep + fname to get full path.
os.listdir gives files and directories names, so we use os.path.isfile to check for files.

like image 82
Kenly Avatar answered Sep 22 '22 16:09

Kenly


Here is another version using the Path module from pathlib instead of os.

def search_in_file(path,searchstring):
    with open(path, 'r') as file:
        if searchstring in file.read():
            print(f'  found string in file {path.name}')
        else:
            print('string not found')

from pathlib import Path

user_input = input('What is the name of your directory')

searchstring = input('What word are you trying to find?')

dir_content = sorted(Path(user_input).iterdir())

for path in dir_content: 
    
    if not path.is_dir():
    
        search_in_file(path, searchstring) 
like image 35
RSale Avatar answered Sep 21 '22 16:09

RSale