I have a gbk of a draft bacterial genomes (meaning multiple contigs).
I want to extract some info about specific genes, and I get an error I cannot understand.

Parse the gbk

    record_dict = SeqIO.to_dict(SeqIO.parse("SpeciesA.gbk", "genbank"))
My file looks:

        for i in record_dict:
        ...     for f in record_dict[i].features:
        ...             print f.qualifiers
    {'locus_tag': ['AA_03640'], 'gene': ['ftsH_4']}
    {'locus_tag': ['AA_03640'], 'inference': ['ab initio prediction:Prodigal:2.6', 'similar to AA sequence:UniProtKB:P37476'], 'codon_start': ['1'], 'EC_number': ['3.4.24.-'], 'transl_table': ['11'], 'product': ['ATP-dependent zinc metalloprotease FtsH'], ........

Now if I try to get out the contig name where the gene AA_03640 is in, as follow:

    for i in record_dict:
    ...     for f in record_dict[i].features:
    ...             if f.qualifiers['locus_tag'] == 'AA_03640':
                        print(i)
But I get the following error:

    Traceback (most recent call last):
      File "<stdin>", line 3, in <module>
    KeyError: 'locus_tag'

Any help to understand where I am wrong?
Thanks

Not all feature types will have a locus tag - you probably only care about CDS or gene features. Even then, not genes will have a locus tag (although generally within a record they either all do, or all don't).

Finally if there is a locus tag, then ``f.qualifiers['locus_tag']`` will return a list (since in general the annotation qualifiers can appear more than once), so you'd want to check using ``'AA_03640' in f.qualifiers['locus_tag']`` or similar, e.g.

    for i in record_dict:
        for f in record_dict[i].features:
            if f.type == "CDS":
                if 'AA_03640' in f.qualifiers.get('locus_tag', []):
                    print(i)

See also http://www.warwick.ac.uk/go/peter_cock/python/genbank which does something similar to what I think you want to achieve.