Welcome, guest | Sign In | My Account | Store | Cart

This recipe demonstrates a simple text file searching program. The code asks for a directory to search (with its subdirectories) and the text that needs to be found in the contained files. This is just a simple starting program demonstrating how to access a computer's file system and accomplish useful work at the same time.

Python, 46 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import os

def main():
    try:
        directory = get_directory()
        text = get_text()
        results = do_search(directory, text)
        finish(results)
    except:
        pass

def get_directory():
    while True:
        try:
            directory = raw_input('Directory: ')
            assert os.path.isdir(directory)
            return directory
        except AssertionError:
            print 'You must enter a directory.'

def get_text():
    while True:
        try:
            text = raw_input('Text: ').lower()
            assert text
            return text
        except AssertionError:
            print 'You must enter some text.'

def do_search(directory, text):
    results = list()
    for dirpath, dirnames, filenames in os.walk(directory):
        for name in filenames:
            full_path = os.path.join(dirpath, name)
            if text in file(full_path).read().lower():
                results.append(full_path)
    return results

def finish(results):
    print
    for filename in results:
        print filename
    raw_input('Done.')

if __name__ == '__main__':
    main()

Regular Expressions could be used to implement this program, but may confuse some people to begin with. If IDLE is not desired for searching files, then the code above can accomplish the like-minded task of finding the file that is difficult to find.

1 comment

Rogier Steehouder 14 years, 8 months ago  # | flag

os.walk(). You should have a look at os.walk() ( http://docs.python.org/lib/os-file-dir.html#l2h-2707 ).