I want to extract genes from genomes
I would highly recommend you to look into the detailed descriptions (or even papers) of how gene prediction and genome annotation is done.
This is a very complicated problem and you seem to missing knowledge of many details that need to be considered, but also there is a lot known about how to do this right, so you don't have to solve this by yourself.

A few very important details, that you either haven't stated or are misunderstanding:


The the ATG codon is the start site of translation, but not the start of a gene. A gene encompasses a promoter (which are very hard to define/predict), 5' &amp; 3' UTRs (untranslated regions) in addition to the open reading frame, which is marked by start &amp; stop codons.
In addition the genomic (i.e. DNA) sequence will contain introns, which are usually much larger than the exons coding for the proteins, and may contain any sequences, so you can not look for codons there.
If you are working with species for which the genome sequence is already known (which is a big number of species), at least a 'rudimentary' gene prediction has very likely already been done. You can find the annotation of all identified features (like genes) in the genome annotation files corresponding to the genome, however this gets very detailed and complicated very fast.
If you are working with a newly sequenced species, where this has not yet been done you need to be aware whether you have genome and/or transcriptome sequences (the latter are important to find gene locations with high quality).