0
有人可以告訴我爲什麼下面的代碼是在指定的路徑中搜索子文件夾。我只想要搜索c:\ Python27中的所有.txt和.log文件。但是,搜索結果顯示了.txt和.log文件在c:\ Python27 \ Doc中......等等等等。謝謝。我定義了一個搜索路徑,但其他文件夾中的文件正在搜索
elif searchType =='3':
print "Directory to be searched: c:\Python27 "
print " "
directory = os.path.join("c:\\","Python27")
regex = re.compile(r'3[0-9]\d{10}')
for root,dirname, files in os.walk(directory):
for file in files:
if file.endswith(".log") or file.endswith(".txt"):
f=open(os.path.join(root,file))
for line in f.readlines():
searchedstr = regex.findall(line)
for word in searchedstr:
print "String found: " + word
print "File: " + os.path.join(root,file)
break
f.close()