Merging and Comparing

Viewed 109

have a directory with files as:

ab_list
bd_list
cd_list
mno_list
hk_list 
pd_list

I have another file called testfile as outside this directory as:

abc
que nw

ab_list   ON   8
gs_list   ON   9
hk_list   OFF  9
bd_list   ON   7
cd_list   OFF  6
fr_list   ON   5
mno_list  ON   4
pq_list   OFF   6
jk_list   ON   7
pd_list   OFF  8

I want to compare the 2 and all the files with filename and ON next to it(if matched) their content should get merged into a new file called merge_file. The other files which match with testfile but have OFF, their filenames should be printed in a new_file. contents of ab_list bd_list and mno_list should get merged into top_file Output should be like new_file:

cd_list OFF no.of lines in file
pd_list OFF no.of lines in file
hk_list OFF no. of lines in file
merge_file (this has all ON merged) no.of lines in file

here is the code till now:

from pathlib import Path

with open('testfile') as fp:
    data = dict([tuple(line.split())for line in fp if line.strip()])

with open('merge_file', 'w') as merge_file, open('match_file', 'w') as match_file:
    lines = 0
    for fp in Path(r'./test').glob('*_list'):
        if fp.name in data:
            if data[fp.name] == 'ON':
                content = fp.open().readlines()
                lines += len(content)

                merge_file.write('\n'.join(content) + '\n')
            else:
                content = fp.open().readlines()
                match_file.write(fp.name + ' OFF {}\n'.format(len(content)))
    match_file.write('merge_file (this has all ON merged) {}'.format(lines))

I want to read from first line but it gives an error called Index Error: list out of range. Currently code reads from 4th line.

1 Answers

Assuming the directory name is Folder, and in that directory is another directory named folder, this code does just that:

from glob import glob

test_file_directory = "C:\\Users\\User\\Desktop\\Folder\\"

files1 = glob("*.txt")
with open(test_file_directory+"testfile.txt","r") as f:
    files2 = [' '.join([l.split()[0],l.split()[1]]) for l in f.readlines()[3:]]

for f1 in files1:
    for f2 in files2:
        if f1[:-4]+'   ON' == f2:
            #print('match')
            with open('merge_file.txt','a') as a:
                with open(f1,'r') as r:
                    a.write(r.read()+'\n')
        elif f1[:-4]+'   OFF' == f2:
            #print('match')
            with open('match_file.txt','a') as a:
                with open(f1,'r') as r:
                    a.write(f"{f2} {len(r.readlines())}\n")
Related