PanDe
PanDe

Reputation: 974

Perform Consecutive 'diff' Operation on Files using Python

Consider I have a scenario like this :

enter image description here

My ultimate objective is to do the following (in_words if image is not clear):

1) Run linux "diff" command between Master_File and Slave_1 File, save it as Diff_1

2) Run linux "diff" command between Master_File and Slave_2 File, save it as Diff_2

3) Run linux "diff" command between Master_File and Slave_3 File, save it as Diff_3

4) Run linux "diff" command between diff_1 and diff_2 File, save it as Semi-Final-Diff

5) Run linux "diff" command between Semi-Final-Diff and diff_3 File and save it as Final_Diff

I tried to achieve this by using 'reduce' from functools, but facing issues.

Let's say I have created all the initial diff's...

from functools import reduce

total_diffs =  ['1','2','3']

def diff_major(a,b):
    out = run_os_command('diff -s '+str(a)+'_'+str(b)+' >diff_'+str(a)+str(b)+'.log')           

def diff_to_diff(total_diffs):
    d = []
    for i in total_diffs:
        d.append('diff_'+str(i)+'.log')
    print d
    reduce(diff_major, d)  

I get the output:

['diff_1.log', 'diff_2.log', 'diff_3.log']
Diffing diff_1.log and diff_2.log
Diffing None and diff_3.log

Why does it blow-off at second diff and changes the name to NONE? Also, how can I achieve this?

Upvotes: 0

Views: 50

Answers (1)

ACascarino
ACascarino

Reputation: 5080

diff_major returns None as it has no return statement.

reduce works by taking the first 2 items in the supplied iterable, in this instance 1 and 2, and providing them as inputs to the supplied function with 2 parameters. It uses the return from this function as the first input to the next iteration; it then takes the next item in the iterable, in this case 3, and provides that as the second input. This repeats until the iterable is exhausted, at which point it returns the final returned value from the supplied function.

Upvotes: 1

Related Questions