Python 3.2 - readline() is skipping lines in source file

Alli OGrady picture Alli OGrady · Jul 8, 2011 · Viewed 14k times · Source

I have a .txt file that I created with multiple lines.

when I run a for loop, with a count accumulator, it skips lines.

It skips the top line, and starts with the second, prints the fourth, the sixth, etc...

What is it I'm missing?

** for your reading pleasure**
def main():
    # Open file line_numbers.txt
    data_file = open('line_numbers.txt', 'r')

    # initialize accumulatior
    count = 1


    # Read all lines in data_file
    for line in data_file:
        # Get the data from the file
        line = data_file.readline()

        # Display data retrieved
        print(count, ": ", line)

        # add to count sequence
        count += 1

Answer

whiskeyfur picture whiskeyfur · Jul 8, 2011

Try removing the "line=data_file.readline()" altogether? I suspect the "for line in data_file:" is also a readline operation.