Split fasta file based on groups in header information and output as separate files
A simple Biopython solution- iterate over the sequence records, identify the species, open the file handle using append mode to ensure no data is overwritten, and write the record:
from Bio import SeqIO

for record in SeqIO.parse(&quot;myfile.fa&quot;, &quot;fasta&quot;):
    species = record.id.split('_')[0]
    with open(f&quot;{species}.fa&quot;, &quot;a&quot;) as f:
        SeqIO.write(record, f, &quot;fasta&quot;)