「リンゴ」のような文字列があります。この文字列を見つけたいのですが、何百ものファイルのうちの 1 つに存在することがわかっています。例えば
file1
file2
file3
file4
file5
file6
...
file200
これらのファイルはすべて同じディレクトリにあります。正確に1つのファイルに含まれていることを知って、Pythonを使用してこの文字列を含むファイルを見つける最良の方法は何ですか.
私はこれを思いついた:
for file in os.listdir(directory):
f = open(file)
for line in f:
if 'apple' in f:
print "FOUND"
f.close()
この:
grep = subprocess.Popen(['grep','-m1','apple',directory+'/file*'],stdout=subprocess.PIPE)
found = grep.communicate()[0]
print found