Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find one file out of many containing a desired string in Python

Tags:

python

I have a string like 'apples'. I want to find this string, and I know that it exists in one out of hundreds of files. e.g.

file1
file2
file3
file4
file5
file6
...
file200

All of these files are in the same directory. What is the best way to find which file contains this string using python, knowing that exactly one file contains it.

I have come up with this:

for file in os.listdir(directory):
    f = open(file)
    for line in f:
        if 'apple' in f:
            print "FOUND"
    f.close()

and this:

grep = subprocess.Popen(['grep','-m1','apple',directory+'/file*'],stdout=subprocess.PIPE)
found = grep.communicate()[0]
print found
like image 286
E.Cross Avatar asked Jun 22 '12 19:06

E.Cross


1 Answers

Given that the files are all in the same directory, we just get a current directory listing.

import os

for fname in os.listdir('.'):    # change directory as needed
    if os.path.isfile(fname):    # make sure it's a file, not a directory entry
        with open(fname) as f:   # open file
            for line in f:       # process line by line
                if 'apples' in line:    # search for string
                    print 'found string in file %s' %fname
                    break

This automatically gets the current directory listing, and checks to make sure that any given entry is a file (not a directory).

It then opens the file and reads it line by line (to avoid problems with memory it doesn't read it in all at once) and looks for the target string in each line.

When it finds the target string it prints the name of the file.

Also, since the files are opened using with they are also automatically closed when we are done (or an exception occurs).

like image 133
Levon Avatar answered Oct 06 '22 01:10

Levon