Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check for EOF in Python?

Tags:

python

eof

How do I check for EOF in Python? I found a bug in my code where the last block of text after the separator isn't added to the return list. Or maybe there's a better way of expressing this function?

Here's my code:

def get_text_blocks(filename):
    text_blocks = []
    text_block = StringIO.StringIO()
    with open(filename, 'r') as f:
        for line in f:
            text_block.write(line)
            print line
            if line.startswith('-- -'):
                text_blocks.append(text_block.getvalue())
                text_block.close()
                text_block = StringIO.StringIO()
    return text_blocks
like image 359
ajushi Avatar asked Jan 03 '10 03:01

ajushi


People also ask

How do you find the EOF in Python?

You can compare the returned value of fp. tell() before and after calling the read method. If they return the same value, fp is at eof.

How do I fix EOF error in Python?

How EOFError can be overcome? We overcome this problem by using keywords like try() and except() in Python. This is referred to as Exception Handling.

What is EOF character in Python?

EOF stands for End Of File. Well, technically it is not an error, rather an exception. This exception is raised when one of the built-in functions, most commonly input() returns End-Of-File (EOF) without reading any data.

Does EOF exist in Python?

EOF stands for End of File. This represents the last character in a Python program. Python reaches the end of a file before running every block of code if: You forget to enclose code inside a special statement like a for loop, a while loop, or a function.


3 Answers

You might find it easier to solve this using itertools.groupby.

def get_text_blocks(filename):
    import itertools
    with open(filename,'r') as f:
        groups = itertools.groupby(f, lambda line:line.startswith('-- -'))
        return [''.join(lines) for is_separator, lines in groups if not is_separator]

Another alternative is to use a regular expression to match the separators:

def get_text_blocks(filename):
    import re
    seperator = re.compile('^-- -.*', re.M)
    with open(filename,'r') as f:
        return re.split(seperator, f.read())
like image 199
Mark Byers Avatar answered Oct 18 '22 17:10

Mark Byers


The end-of-file condition holds as soon as the for statement terminates -- that seems the simplest way to minorly fix this code (you can extract text_block.getvalue() at the end if you want to check it's not empty before appending it).

like image 26
Alex Martelli Avatar answered Oct 18 '22 19:10

Alex Martelli


This is the standard problem with emitting buffers.

You don't detect EOF -- that's needless. You write the last buffer.

def get_text_blocks(filename):
    text_blocks = []
    text_block = StringIO.StringIO()
    with open(filename, 'r') as f:
        for line in f:
            text_block.write(line)
            print line
            if line.startswith('-- -'):
                text_blocks.append(text_block.getvalue())
                text_block.close()
                text_block = StringIO.StringIO()
         ### At this moment, you are at EOF
         if len(text_block) > 0:
             text_blocks.append( text_block.getvalue() )
         ### Now your final block (if any) is appended.
    return text_blocks
like image 1
S.Lott Avatar answered Oct 18 '22 17:10

S.Lott