Skip to content Skip to sidebar Skip to footer

"list Index Out Of Range" When Try To Output Lines From A Text File Using Python

I was trying to extract even lines from a text file and output to a new file. But with my codes python warns me 'list index out of range'. Anyone can help me? THANKS~ Code: f = ope

Solution 1:

After

num_lines = sum(1 for line in f)

The file pointer in f is at the end of the file. Therefore any subsequent call of f.readlines() gives an empty list. The minimal fix is to use f.seek(0) to return to the start of the file.

However, a better solution would be to read through the file only once, e.g. using enumerate to get the line and its index i:

newline = []
for i, line inenumerate(f):
    if i % 2 == 0:
        newline.append(line)

Solution 2:

In your original script you read the file once to scan the number of lines, then you (try to) read the lines in memory, you needlessly create a list for the full size instead of just extending it with list.append, you initialize the list with zeroes which does not make sense for a list containing strings, etc.

Thus, this script does what your original idea was, but better and simpler and faster:

withopen('input.txt', 'r') as inf, open('output.txt', 'w') as outf:
    for lineno, line inenumerate(inf, 1):
        if lineno % 2 == 0:
            outf.write(line)

Specifically

  • open the files with with statement so that they are automatically closed when the block is exited.
  • write as they are read
  • as lines are numbered 1-based, use the enumerate with the start value 1 so that you truly get the even numbered lines.

Solution 3:

You've also got the itertools.islice approach available:

from itertools import islice

withopen('input') as fin, open('output', 'w') as fout:
    fout.writelines(islice(fin, None, None, 2))

This saves the modulus operation and puts the line writing to system level.

Post a Comment for ""list Index Out Of Range" When Try To Output Lines From A Text File Using Python"