python写了个小工具

源码太多,看的头疼。在目录里找含有某个字符串的文件。结果写到result.txt中

import os
import codecs


fileinfo=open('result.txt','w')  #结果目标文件

#遍历目录,查找目标,将结果写到result.txt中
# dir 目录
# target 要查找的字符串
# type 文件类型
#
def walk_dir(dir,target,type,topdown=True):   
for root, dirs, files in os.walk(dir, topdown):
for name in files:
if name.find(type)==(len(name)-len(type)):
f=codecs.open(os.path.join(root,name),'r','UTF-8')
for line in f.readlines():
if line. find(target)!=-1:
print (os.path.join(root,name))               
fileinfo.write('  ' + os.path.join(root,name) + '\n')
#dir = raw_input('please input the path:')
#target=raw_input('please input the target:')
#type=raw_input('please input the file type:')

dir=r"C:\Users\T\Documents\Flex Builder 3\Pegasus"  #目录
target="Dlg_Edit_Supplier"                          #目标字符串
type="as"                                           #文件类型
walk_dir(dir,target,type)

参数直接写在里面
原文地址:https://www.cnblogs.com/macula7/p/1960410.html