title: Merging and Comparing
body: <p>have a directory with files as:</p>
  <pre><code>ab_list
  bd_list
  cd_list
  mno_list
  hk_list 
  pd_list
  </code></pre>
  <p>I have another file called testfile as outside this directory as:</p>
  <pre><code>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
  </code></pre>
  <p>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:</p>
  <pre><code>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
  </code></pre>
  <p>here is the code till now:</p>
  <pre><code>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))
  
  </code></pre>
  <p>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.</p>