Python - Read a File Line-by-Line

1 min read

Pythonic way

The most Pythonic way to open a file and iterate through each line is like this:

with open('file.txt') as f:
    for line in f:
        # do something

Fast way

The fast way to read the lines of a file into a list with Python is to use the file object's readlines() method:

with open('file.txt') as f:
    lines = f.readlines()

# Takes approx. 0.03 of a second with a file # that has 479k single words, each on a new line.

Stripping newlines

If you want to strip the newline character \n from each line when adding it to a list you can use the strip() method within a list comprehension:

with open('file.txt') as f:
    lines = [ line.strip() for line in f ] # Takes approx. 0.10 of a second with a file # that has 479k single words, each on a new line.

If you're thinking about combining readlines() and strip() at this point, just know that it takes a bit longer than the simple list comprehension above and is also not as concise:

with open('file.txt') as f:
    lines = f.readlines()
    clean = [ line.strip() for line in content ] # Takes approx. 0.11 of a second with a file # that has 479k single words, each on a new line.

Finally, if you want to use a for loop you can do so like this:

with open('file.txt') as f:
    lines = []
    for line in f:
        lines.append(line.strip())

# Takes approx 0.13 of a second with a file 
# that has 479k single words, each on a new line.


Author picture
Rhett Trickett

Founder at Able

@rhett RhettTrickett rhetttrickett
Python

Join the discussion

Able is a developer community where people build their coding knowledge and careers.

Join with GitHub Join with Twitter