# -*- coding: cp936 -*-from Tkinter import *import Tkinterimport os
# 目的:查找一个目录下的包含指定字符串的文件
def my_find(dir_name, find_str, results): myfiles = os.listdir(dir_name) for myfile in myfiles: file_name = dir_name + "//" + myfile if os.path.isfile(file_name): f = open(file_name, 'r') filer = f.read() if(find_str in filer): results.append(file_name) f.close() elif os.path.isdir(file_name): my_find(file_name, find_str, results) return results
def find(): dir_name = e.get() find_str = e2.get() results = [] my_find(dir_name, find_str, results) myresult = '' for result in results: myresult = myresult + result + '/n' edit1.insert(END, myresult) root = Tk()
L1 = Label(root, text ='Please input the dir you want to find').pack()e = StringVar()entry = Entry(root,textvariable = e, width = 60)entry.pack()
L2 = Label(root, text ='Please input the sting you want to find').pack()e2 = StringVar()entry2 = Entry(root,textvariable = e2, width = 60)entry2.pack()
Button(root,text = 'Find',command = find).pack()
edit1 = Tkinter.Text(root, selectbackground = 'red', selectforeground = 'gray')edit1.pack()
root.mainloop()