Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- " Created by Daniele Costarella <daniele.costarella@gmail.com>
- " Search file contents in multiple subdirectories
- """
- import os
- def find(word):
- def _find(path):
- with open(path, "rb") as fp:
- for n, line in enumerate(fp):
- if word in line:
- yield n+1, line
- return _find
- def search(word, start):
- finder = find(word)
- for root, dirs, files in os.walk(start):
- for f in files:
- path = os.path.join(root, f)
- for line_number, line in finder(path):
- yield path, line_number, line.strip()
- if __name__ == "__main__":
- import sys
- if not len(sys.argv) == 3:
- print("USAGE: python find.py word directory")
- sys.exit(1)
- word = sys.argv[1]
- start = sys.argv[2]
- for path, line_number, line in search(word, start):
- print ("{0} matches in line {1}: '{2}'".format(path, line_number, line))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement