我的程序存在一个小问题。 程序序列:
.xml
的文件,并能够搜索是否有我输入的字符串。我的程序在使用.txt
个文件时效果很好,但是当我在xml
个文件中搜索它时效果不佳
也许有人可以帮助我?
以下是代码:
class App(object):
def __init__(self):
global dirbut
self.root = Tk()
self.root.wm_title("BarcodeCheck")
self.label = Label(self.root,text = " First open your XML folder.")
self.label = Label (self.root, text="First open your XML folder.")
self.label.pack()
self.buttontext = StringVar()
self.buttontext.set(dirtext)
button_opt = {'fill': Tkconstants.BOTH, 'padx': 5, 'pady': 5}
Button(self.root, textvariable=self.buttontext, command=self.opened).pack(**button_opt)
#dirbut = Button(self.root, textvariable=self.buttontext, command=self.opened).pack(**button_opt)
self.dir_opt = options = {}
options['initialdir'] = 'C:\\'
options['mustexist'] = False
options['parent'] = self.root
options['title'] = 'blablabla '
self.label = Label (self.root, text="")
self.label.pack()
self.label = Label (self.root, text= "Enter your Barcode.")
self.label.pack()
self.entrytext = StringVar()
Entry(self.root, textvariable=self.entrytext).pack()
self.buttontext = StringVar()
self.buttontext.set("Check")
Button(self.root, textvariable=self.buttontext, command=self.clicked1).pack()
self.label = Label (self.root, text="")
self.label.pack()
self.root.mainloop()
# here is the PROBLEM
# it shoud open all files with ending .xml (here with txt because it works)
#end then search the hole directory if there is "input"(the string)-
#in f(all opened xml files)
def clicked12(self):
directory = data
for root,dirs,files in os.walk(data):
for file in files:
if file.endswith(".txt"):# it shoud work with .xml but it doesnt
f=open(file, 'r')
# content = f.read()
for line in f:
#line= line.rstrip()
if input in line:
print "---------------------------------------------"
result = "Barcode was found\nhere: " +os.path.join(root,file)
print file +" found\n here: " +os.path.join(root,file) +"\n"+"\n"+"\n"
print input
else:
result = "not found, please check XML file"
print file + " false " #+ "!!!!"+input+"!!!!!" + os.path.join(root,file)
#print"--------------------------------------------"
self.label.configure(text=result)
print "#################################################"
def button_click(self, e):
pass
def opened(self):
global data
data = tkFileDialog.askdirectory(**self.dir_opt)
# dirbut["text"] = str(data) if data else dirtext
buttontext = str(data)
if data is "":
result = "XML file not found"
else:
result = "directory:"+ str(data)
self.label.configure(text=result)
def clicked1(self):
global input
input = self.entrytext.get()
if len(input) != 9:
result = "Barcode consists of 9 characters\ne.g: AUG456789 "
else:
result = self.clicked12()
self.label.configure(text=result)
App()