Python - How to recognize EOL in FASTA file

199 views Asked by At

I have a python script that needs to read through a FASTA file character-by-character. To do this I have been using c = f.read(1), where f is the opened file. I need it to recognize when it reaches the end of a line, but '\n' and '\r' don't seem to work. I have never worked with this file type before so I'm not sure if FASTA is different. Does anyone know how to do this? Thanks!

Edit: EOF != EOL. I know how to find the end of file.

Edit 2: The line I'm using to search for the EOL character is this:

if c == '\n' or c == '\r':
    #operations to perform if found...
1

There are 1 answers

1
Robᵩ On BEST ANSWER

Open the file in universal newline mode:

with open('myfile.fasta', 'rU') as fp:
   ...

and compare the character against '\n':

if c == '\n':