title: How to match a specific part of an input file to that of another file so that I can copy contents from the input to the second file
body: <p>I have a dir with input files (40 total). The file names are all the same except for a number identifier.</p>
  <ol>
  <li>Input dir example:</li>
  </ol>
  <ul>
  <li>File1 = msl_<strong>gene001181</strong>_aln.nexus</li>
  <li>File2 = msl_<strong>gene001650</strong>_aln.nexus</li>
  <li>File3 = msl_<strong>gene001976</strong>_aln.nexus</li>
  <li>etc.</li>
  </ul>
  <p>I have a second dir files (40 total) named with the same number identifiers.</p>
  <ol start="2">
  <li>Second dir example:</li>
  </ol>
  <ul>
  <li>File1 = bipartitions_<strong>gene001181</strong>.nexus</li>
  <li>File2 = bipartitions_<strong>gene001650</strong>.nexus</li>
  <li>File3 = bipartitions_<strong>gene001976</strong>.nexus</li>
  <li>etc.</li>
  </ul>
  <p>I need to append information from my input files into the files of the second dir with the same matching number identifiers.</p>
  <p>For example, information from <em>msl_gene001181_aln.nexus</em> needs to go into <em>bipartitions_gene001181.nexus</em>.</p>
  <p>I just do not know how to write the code so that the contents of one file get appended to the correct file in the second directory. This is the last part of a larger script that I am writing. I got stuck at the end.</p>
  <p>I appreciate any help.</p>
  <p><strong>EDIT</strong></p>
  <ul>
  <li>Logical steps:</li>
  </ul>
  <ol>
  <li>Read into my input files.</li>
  <li>Once I am able to read into my input files, I need to extract specific lines of text.</li>
  <li>Take the extracted lines of text and <strong>append</strong> them to the outfile with the same matching number identifier in the filename.</li>
  <li>Code needs to tell the computer that the information from the input file with <em>gene001181</em> in its file name needs to be appended to the output file that also contains <em>gene001181</em>.</li>
  </ol>
  <pre><code>import os
  
  path_to_input_data = &quot;path/&quot;
  input_dir = os.listdir(path_to_input_data)
  
  output_dir = &quot;output_dir&quot;
  if not os.path.exists(output_dir):
  os.makedirs(output_dir)
  
  for input_files in input_dir:
      if input_files.endswith(&quot;.nexus&quot;):
          input_file_path = os.path.join(path_to_input_data, input_files)
          input_nexus_files = open(input_file_path, 'r')
          with open(os.path.join(output_dir, file), 'a') as output:
              for lines in nexus_file:
                  if lines.startswith(&quot;begin data&quot;):
                      output.write(lines)
                      try:
                          while &quot;end&quot; not in lines:
                              next_line = next(nexus_file)
                              output.write(next_line)
                      except StopIteration:
                          pass
  
  </code></pre>
  <p>This bit of code extracts the information that I need, and it creates new output files with the input information in the output dir. I would like to extract my lines of text from my input files, and put them in the files that are <strong>already</strong> in my output dir. The input files and output files have matching numbers. I just do not know how to write that part in my code.</p>
  <p>I apologize for the lack of info in my original post, and I appreciate the help.</p>