All pastes #1228896 Raw Edit

Stuff

public text v1 · immutable
#1228896 ·published 2008-10-16 23:59 UTC
rendered paste body
#!/usr/bin/env python

# filefinder.py
# 	written by mgroman

#		arg 1 = the file or path
#		arg 2 = the keyword to search for
#
#		the script will search arg 1 for arg 2 and print the line number and path of each
#		correct match

import sys
import os


# get path
path = sys.argv[1]
keyword = sys.argv[2]
files = []

print "searching for " + keyword + "..."

if os.path.exists(path):
	# recursively get all files
	if os.path.isfile(path):
		print "its a file"
		files.append(path)
	else:
		print "its a path..."
		for root, dirs, files in os.walk(path):
			print "root  => ", root
			print "dirs  => ", dirs
			print "files => ", files
			print "\n\n"
else:
	# wtf did they pass?
	print path + " is not a valid arg!"
	sys.exit(1);

for file in files:
	line_count = 0
	file_handle = open(file)
	for line in file_handle.readlines():
		line_count += 1
		if line.find(keyword) > 0:
			print line

	file_handle.close()
	print os.path.abspath(file) + " has " + str(line_count) + " lines!"