I have the following code in python:


from Bio import SeqIO

count = SeqIO.convert(“genome1.gbk”, “genbank”, “genome1.fasta”, “fasta”)

print("Converted %i records" % count)

This code converts the genbank file "genome1.gbk" to a fasta file "genome1.fasta". But now I want to convert all files in the current folder with this code. All the files in the current folder are genbank files and I want them to turn into fasta files with this code. I was thinking of using wildcards, but I don't know exactly how to change this code. Any help is appreciated

  • Are there nested folders with files? – RomanPerekhrest Nov 26 '19 at 17:31
  • Nope, only genbank files. – tucanco Nov 26 '19 at 18:46

With Python's os.scandir feature:


from Bio import SeqIO
from os import scandir

with scandir() as it:
    for entry in it:
        if entry.name.endswith('.gbk') and entry.is_file():
            count = SeqIO.convert(entry.name, 'genbank', '{}.fasta'.format(entry.name[:-4]), 'fasta')
            print("Converted %i records" % count)
  • 1
    it worked wonderfully !! – tucanco Nov 26 '19 at 20:11

You may follow this code:

import os 
from Bio import SeqIO

for filename in os.listdir('.'):
  if filename.endswith(".gbk"):
    count = SeqIO.convert(filename, “genbank”, "{}.fasta".format(entry.name[:-4]), “fasta”)
    print("Converted %i records" % count)


  • But in this case, all the outputs would have the same name (genome1.fasta) I'm right?. This code overwrite the output files with the same name and I don't want that. All the output files must have the same input names, but instead .gbk it must be .fasta – tucanco Nov 26 '19 at 19:06
  • OK, got it, fixed my answer – Shahar Gino Nov 29 '19 at 12:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.