複数の文字列が含まれてファイルを検索

仕様:指定されたディレクトリとファイルの種類の下で複数の文字が含まれてファイルを検索する
実行コマンド:python getfile.py  パス ファイルタイプ  キーワード  多数のキーワードの中にスペースがある
例えば:python getfile.py d:\java .java

#encoding:shift-jis
import os, sys

find_text = sys.argv

diro = find_text[1]
filetype = find_text[2]

print '\n検索ディレクトリ %s’ % diro
print '検索ファイルタイプ %s’ % filetype
print 'キーワード: ',’ '.join(find_text[3:])

print '\n\nキーワードが含まれているファイルをクエリされた:\n’
for root, subdirs, filename in os.walk(diro):
for i in filename :
if (i.endswith(filetype)):
infile = open(root+’\\’+i, 'r’)
content = infile.read().lower()
for j in find_text[3:]:
if j in content:
sts = True
else:
sts = False
break
if sts == True:
print root+’\\’+i

Source

Posted by arkgame