I have this function in my code:
def load_fasta(filename):
f = open(filename)
return (seq.group(0) for seq in re.finditer(r">[^>]*", f.read()))
This will leave the file open indefinitely, which isn't good practice. How do I close the file when the generator is exhausted? I guess I could expand the generator expression into a for loop with yield statements and then close the file afterwards. I'm trying to use functional programming as often as possible, though (just as a learning exercise). Is there a different way to do this?