user765443
user765443

Reputation: 1892

compare two file with specified line

We have two log file in which one is generated from 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.

Upvotes: 0

Views: 103

Answers (2)

Eric
Eric

Reputation: 97691

For something this simple:

with open('golden_file') as afile, open('process_file') as bfile:
    matches = len(afall(aline == bline for aline, bline in zip(afile, bfile))

    # check the golden file iterator is exhausted
    if any(afile):
        matches = False

Taking advantage of the fact that zip returns an iterable matchin the length of the shortest

Upvotes: 1

DhruvPathak
DhruvPathak

Reputation: 43265

Use difflib . It would do most of the things of command line diff and more.

Upvotes: 0

Related Questions