I rewrote you code into a function that can be called using each filename you have, possibly collected into a list using os.listdir()
.
from Bio import SeqIO
def parse_file(filename):
new_name = f"rc_{filename}"
with open(new_name, "w") as new:
for rec in SeqIO.parse(filename, "fasta"):
print(rec_id:=rec.id)
print(rev_comp:=str(rec.seq.reverse_complement()))
new.write(f">rc_{rec_id}\n{rev_comp}\n")
I used f-strings to create both the new filename and the strings written to that file. I also used the "walrus operator" to assign the values of rec.id
and rec.seq.reverse_complement()
to temp variables so we don't have to run those operations again when we write the data. This will save compute cycles and time over the long run. However, use of :=
means the code will only run under Python 3.8 and later.