# How do I rewrite a read group using pysam?

I am trying to rewrite a SAM/BAM file with altered read group entries using pysam. In this simplified version, I want to take a BAM, and rewrite the SM tags in all read groups to the same string and copy the alignments with new header into a new BAM. The contents of the read group dictionaries I am changing appear to have no effect on the header that prints or is written to file output. How can I alter the read groups?

import argparse
import pysam

try:
bam_input = pysam.AlignmentFile(input_filename, 'rb')
finally:
bam_input.close()

try:
bam_input = pysam.AlignmentFile(input_filename, 'rb')

# copy alignments

finally:
bam_input.close()
bam_output.close()

if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Rewrite bam read groups")