2
\$\begingroup\$

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.

\$\endgroup\$
3
  • 2
    \$\begingroup\$ Just to be sure, you want to compare the 2 files after deleting the last line from 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 the Golden File? \$\endgroup\$ Commented Sep 7, 2013 at 14:11
  • 1
    \$\begingroup\$ 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 type print data == data_1 \$\endgroup\$
    – Stuart
    Commented Sep 7, 2013 at 15:00
  • \$\begingroup\$ @AseemBansal, correct Ya I am planning to remove last line from golden also \$\endgroup\$
    – user765443
    Commented Sep 7, 2013 at 15:32

2 Answers 2

1
\$\begingroup\$

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"
\$\endgroup\$
2
  • \$\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
    Commented 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 whether lines_match is true. \$\endgroup\$
    – Stuart
    Commented Sep 7, 2013 at 16:16
1
\$\begingroup\$

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")
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.