<p>这将帮助您:</p>
<pre><code>wfiles = ['a.txt', 'b.txt', 'c.txt']
ylist = ['grapes', 'name']
for w in wfiles:
with open(w) as ofiles:
content = ofiles.read()
if any(word in content for word in ylist):
print w, 'FOUND'
else:
print w, 'NOTFOUND'
</code></pre>
<p>这对我很有用:</p>
<pre><code>import os
something = '.'
ylist = ['grapes', 'name']
dmd = os.listdir(something)
wfiles = []
for item in dmd:
if ".txt" in item:
wfiles.append(item)
for w in wfiles:
with open(something + '/' + w) as ofiles:
content = ofiles.read()
if any(word in content for word in ylist):
print w, 'FOUND'
else:
print w, 'NOTFOUND'
</code></pre>
<p>如果仍然得到意外结果,请检查脚本是否打开了预期的正确文件。你知道吗</p>