擅长:python、mysql、java
<p>使用glob作为替代,您可以筛选特定的文件名、扩展名或目录中的所有文件。在</p>
<pre><code>>>> from glob import glob
>>> key = 'hello'
>>> for file in glob("e:\data\*.txt"):
with open(file,'r') as f:
line_no = 0
for lines in f:
line_no+=1
if key.lower() in lines.lower():
print "Found in " + file + "(" + str(line_no) + "): " + lines.rstrip()
Found in e:\data\data1.txt(1): Hello how are you
Found in e:\data\data2.txt(4): Searching for hello
Found in e:\data\data2.txt(6): 3 hello
</code></pre>