【发布时间】:2014-04-24 19:44:21
【问题描述】:
我需要在一个目录中搜索几千个单独的文本文件,看看有多少包含一个字符串。我需要使用python。现在我有以下基本代码适用于一个单独的文件。我不知道下一步:如何遍历目录中每个单独文件的内容。这是我所拥有的:
stringtofind = 'FULL TEXT' #enter something between the ''s
filetolookin = '2013-04-061.txt' #enter the file you want to search
def countif(isthis, infile):
count = 0
if isthis in open(infile).read():
count = 1+count
return count
else:
return count
print countif(stringtofind, filetolookin)
感谢您的帮助。
【问题讨论】:
标签: python loops if-statement python-2.x