Question

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...
Was it helpful?

Solution

Open the file in universal newline mode:

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

and compare the character against '\n':

if c == '\n':
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top