We have two log files in which one is generated from a process. We compare one file(golden file) to another file to check if file correct or not. It should have same value. We generally use diff utility to compare two files. I have got enhancement to add machine information into process generated File. So I want to compare upto previous line and ignore new changes. Could Anyone provide me any utility which I can use in python.
Golden File
CMD gen -vdd 0.99 -vss 0 -sinps 0.02 -slew 0.1 -temp -40 -rise -data
CMD gen -vdd 0.99 -vss 0 -sinps 0.06 -slew 0.1 -temp -40 -rise -clock
CMD gen -vdd 0.99 -vss 0 -sinps 0.02 -slew 0.1 -temp -40 -fall -data
CMD gen -vdd 0.99 -vss 0 -sinps 0.02 -slew 0.1 -temp -40 -fall -data
CMD gen -vdd 0.99 -vss 0 -sinps 0.06 -slew 0.1 -temp -40 -rise -clock
CMD gen -vdd 0.99 -vss 0 -sinps 0.02 -slew 0.1 -temp -40 -rise -data
Temp1 Temp2 Temp3 Temp4 Temp5 Temp6
-31.00 -19.00 -3.00 -8.00 43.00 61.00
Process File
CMD gen -vdd 0.99 -vss 0 -sinps 0.02 -slew 0.1 -temp -40 -rise -data
CMD gen -vdd 0.99 -vss 0 -sinps 0.06 -slew 0.1 -temp -40 -rise -clock
CMD gen -vdd 0.99 -vss 0 -sinps 0.02 -slew 0.1 -temp -40 -fall -data
CMD gen -vdd 0.99 -vss 0 -sinps 0.02 -slew 0.1 -temp -40 -fall -data
CMD gen -vdd 0.99 -vss 0 -sinps 0.06 -slew 0.1 -temp -40 -rise -clock
CMD gen -vdd 0.99 -vss 0 -sinps 0.02 -slew 0.1 -temp -40 -rise -data
Temp1 Temp2 Temp3 Temp4 Temp5 Temp6
-31.00 -19.00 -3.00 -8.00 43.00 61.00
Adding machine name( ignore machine name)
I have write code in following.Can we better way for improve code
data = None
with open("Golden_File",'r+') as f:
data = f.readlines()
del data[-1]
data_1 = None
with open("cp.log",'r+') as f:
data_1 = f.readlines()
del data_1[-1]
print cmp(data, data_1)
[Question]: Does cmp function works fine in list also. I have used first time and not sure how internally works.
2 Answers 2
If you don't mind reading the entire files into memory at once (as you are currently doing), you can just compare them as lists of lines:
with open("Golden_File", "r") as golden, open("cp.log", "r") as log:
if golden.readlines() == log.readlines()[:-1]:
print "files match"
Otherwise you can use itertools
and compare one line at a time:
from itertools import izip
with open("Golden_File", "r") as golden, open("cp.log", "r") as log:
lines_match = all(a == b for a, b in izip(golden, log))
one_line_left = len(list(log)) == 1 and list(golden) == []
if lines_match and one_line_left:
print "files match"
-
\$\begingroup\$ Agree, But As I am doing operation on the last line. And I know only last line will be change than it will be overhead for me \$\endgroup\$user765443– user7654432013年09月07日 16:09:50 +00:00Commented Sep 7, 2013 at 16:09
-
\$\begingroup\$ Not sure what you mean - one way or another, you have to read the whole file in. If you don't need to check that there is a remaining line in the log file, you can remove
one_line_left = ...
and just check whetherlines_match
is true. \$\endgroup\$Stuart– Stuart2013年09月07日 16:16:56 +00:00Commented Sep 7, 2013 at 16:16
There is no need for python here, you can use the head
command to filter out the last line of a file:
diff <(head -n -1 "Golden_File") <(head -n -1 "cp.log")
Process File
. Correct? If correct then why are you deleting the last line in data. Wouldn't that delete the last line from what you have read from theGolden File
? \$\endgroup\$cmp
does indeed work for lists - see docs.python.org/2/tutorial/… and docs.python.org/2/library/functions.html#cmp. But you could just as easily typeprint data == data_1
\$\endgroup\$