user8845744
user8845744

Reputation:

Not iterating through lines of text file properly

I have this function that opens a txt file containing 1 string of characters on each line. It doesn't seem to be working properly. When I test it to call the line of text (gene_line) it is returning the bottom-most line of text when it should be the first line of text.

Upvotes: 1

Views: 50

Answers (2)

Blender
Blender

Reputation: 298076

Iterating through file once exhausts the iterator completely, meaning your second attempt at iterating through the file will not actually do anything. You need to "rewind" the file handle back to the beginning before reading it again:

file.seek(0)

# iterates through lines in file again to find mask matches now
for aline in file:
    ...

Upvotes: 1

Vijay K. Narayanan
Vijay K. Narayanan

Reputation: 84

The code looks right. If you call or print gene_line outside of the for loops, then it is only going to print the last assigned value of the gene_line variable, which would be the last line of the file.

Upvotes: 0

Related Questions