As an exercise, I have been asked to compare 5 ascii files but from different paths. The problem is that the files have different names (in one folder they are called one way and in the other another, but the content is the same). Actions must be performed using buttons. I managed to get them to be compared but: 1. Choosing the paths in "order", if they are chosen the other way around it no longer works 2. It is done using many for The idea is not to do a for loop for each file tuple, but to exit in a single run. I tried to do it through dictionaries but it doesn't work for me. 3. If the files are different, it tells you which file and which line is different, and if not, it should only print "IDENTICAL" I am a beginner in python and I would really appreciate if you gave me a solution. I attach my code
def openSelectFile1():
global file_path
root = Tkinter.Tk() #close the window by tkinter
root.withdraw() #close
file_path = tkFileDialog.askdirectory() #open the file explorer and save the selection in the variable
def openSelectFile2():
global file_path2
root = Tkinter.Tk() #close the window by tkinter
root.withdraw() #close
file_path2 = tkFileDialog.askdirectory() #open the file explorer and save the selection in the variable
def compareBothFiles():
#dic={1:['/CORE0.asc', '/CORE1.asc', '/EEPROM_EXT.asc', '/ETS.asc', '/FBL.asc']} `nombres de archivos ascii de la primera ruta`
#dic2={2:['/CHR_ORC_C0_Appl_opt.asc', '/CHR_ORC_C1_opt.asc', '/CHR_ORC_ETS_opt.asc', '/CHR_ORC_FBL_Appl_opt.asc', '/EEPROM_Ext_EMPTY.asc']} `nombres de archivos ascii de la segunda ruta`
#values= dic.values()
#values2= dic2.values()
#print(values)
#f1 = open(file_path+values)
#f2 = open(file_path2+values2)
f1 = open(file_path+'/CORE0.asc')
f2 = open(file_path2+'/CHR_ORC_C0_Appl_opt.asc')
i=0
for line1 in f1:
i += 1
for line2 in f2:
if line1 != line2:
print("Line:", i, "= DIFFERENT")
print("difference in: ", '/CORE0.s37')
print("difference in: ", '/CHR_ORC_Appl_opt.s37')
else:
print(" ")
break
f1 = open(file_path+'/CORE1.asc')
f2 = open(file_path2+'/CHR_ORC_C1_opt.asc')
i=0
for line1 in f1:
i += 1
for line2 in f2:
if line1 != line2:
print("Line:", i, "= DIFFERENT")
print("difference in: ",'/CORE1.asc')
print("difference in: ",'/CHR_ORC_C1_opt.asc')
else:
print(" ")
break
f1 = open(file_path+'/EEPROM_EXT.asc')
f2 = open(file_path2+'/EEPROM_Ext_EMPTY.asc')
i=0
for line1 in f1:
i += 1
for line2 in f2:
if line1 != line2:
print("Line:", i, "= DIFFERENT")
print("difference in: ",'/EEPROM_EXT.asc')
print("difference in: ",'/EEPROM_Ext_EMPTY.asc')
else:
print(" ")
break
f1 = open(file_path+'/ETS.asc')
f2 = open(file_path2+'/CHR_ORC_ETS_opt.asc')
i=0
for line1 in f1:
i += 1
for line2 in f2:
if line1 != line2:
print("Line:", i, "= DIFFERENT")
print("difference in: ",'/ETS.asc')
print("difference in: ",'/CHR_ORC_ETS_opt.asc')
else:
print(" ")
break
f1 = open(file_path+'/FBL.asc')
f2 = open(file_path2+'/CHR_ORC_FBL_Appl_opt.asc')
i=0
for line1 in f1:
i += 1
for line2 in f2:
if line1 == line2:
print('')
else:
print("Line ", i, ": DIFFERENT")
print("difference:", '/FBL.asc')
print("difference:", '/CHR_ORC_FBL_Appl_opt.asc')
break
def quit():
root.destroy()
if __name__ =='__main__':
root = Tk()
Label(root, text="COMPARE").pack(side=TOP)
compareFrame = LabelFrame(root, text = "Selection your files to compare", padx=5, pady=5)
compareFrame.pack(padx=10, pady=10)
Button(root, text="Compare", padx=30, pady=5, command=compareBothFiles).pack()
Label(root, text="").pack()
Button(compareFrame, text="File 1", padx=70, pady=5, command=openSelectFile1).grid(row=1, column=1)
Button(compareFrame, text="File 2", padx=70, pady=5, command=openSelectFile2).grid(row=2, column=1)
Label(root, text="").pack()
Button(root, text="close",bg= "lightgray", padx=15, pady=5, command=quit).pack(side=BOTTOM)
root.resizable(width=True, height=True)
root.title("HexView and Compare")
root.mainloop()
In Python, as in any other language, it is possible to pass parameters to a function so that it operates on a wide variety of values. Passing parameters via global variables is a profound error.
In particular, we can write a function that compares two arbitrary files and tells us if they are the same or different.
This function does not depend on global variables or anything external; it is totally self-contained; you just have to provide the names of the files.
This function will receive the names of the files (with the full path) and will return
True
orFalse
depending on whether they are the same or different:The procedure is simple: open the files and read one line from each and compare them. If they are different, it returns
False
. If they are the same, continue with the next pair, unless we have reached the end of the file. This is detected because the read line has zero length.show
File read.txt
File read2.txt
Code
produces: