VMware ESXi and vSphere Cluster Management

Read a File into a List of Lines in Python

Learn how to use Python open(), with, and readlines() to load a text file into a list, process lines, number them, and clean whitespace safely.

Python can represent the contents of a text file as a list of line strings. This is useful when you need to inspect, search, number, sort, or otherwise process all lines after reading them.

This lesson uses open(), a with statement, and the file-object method readlines(). You should already know basic variables, strings, lists, for loops, file paths, and how to run a Python script.

Prepare a Sample Text File

Create a UTF-8 plain-text file named new_file.txt in the same folder as your Python script. Its contents can be:

Have a nice day!
You too!
Thanks!

The final line may or may not have a line-ending character, depending on how the file was saved. This difference is visible when Python displays the resulting list.

Open a Text File Safely

open() is a built-in function that opens a file and returns a file object. A file object represents the open file and provides methods for reading its content.

Use a with statement when opening a file. A with statement is a context-management construct that automatically closes the file when the indented block finishes, including when an error occurs inside the block.

with open("new_file.txt") as file:
    lines = file.readlines()

For reading text, the default mode is "r". Therefore, this is equivalent:

with open("new_file.txt", "r") as file:
    lines = file.readlines()

Using "r" explicitly can make the purpose clearer. You can also specify the expected text encoding, especially when sharing code between systems:

with open("new_file.txt", "r", encoding="utf-8") as file:
    lines = file.readlines()

Create a List with readlines()

readlines() reads the remaining lines from an open file object and returns them as a Python list. A list is an ordered, mutable collection. Each list element is a string representing one line from the file.

with open("new_file.txt", encoding="utf-8") as file:
    lines = file.readlines()

print(lines)

If the first two source lines end with newline characters and the final line does not, representative output is:

['Have a nice day!\n', 'You too!\n', 'Thanks!']

When Python displays a list of strings, the newline character is shown using the escape sequence \n. The characters \n in this representation stand for one newline character inside the string; they are not normally two separate backslash and n characters.

Line endings are normally retained when they are present in the source file. If the file does not end with a newline, its final list element does not include \n. That is normal and reflects the actual file content.

Process Each Line with a for Loop

Once the list has been created, use a for loop to visit each string:

with open("new_file.txt", encoding="utf-8") as file:
    lines = file.readlines()

for line in lines:
    print(line, end="")

Each loop iteration assigns one list element to line. The end="" argument prevents print() from adding an extra newline, because a line read from the file usually already contains one.

You can also process the list after the file has closed:

with open("new_file.txt", encoding="utf-8") as file:
    lines = file.readlines()

for line in lines:
    if "day" in line:
        print("Found:", line, end="")

The file is closed when the with block ends, but the strings in lines have already been copied into memory.

Number Lines with enumerate()

enumerate() supplies both an item and its position during iteration. Use start=1 for human-friendly line numbers:

with open("new_file.txt", encoding="utf-8") as file:
    lines = file.readlines()

for line_number, line in enumerate(lines, start=1):
    print(f"{line_number}: {line}", end="")

On the first iteration, line_number is 1 and line is the first string. On the next iteration, the number is 2 and the line is the second string, and so on.

Example output:

1: Have a nice day!
2: You too!
3: Thanks!

Remove Newline Characters and Whitespace

Using strip()

strip() returns a copy of a string with whitespace removed from both ends. This includes newline characters, spaces, and tabs.

with open("new_file.txt", encoding="utf-8") as file:
    lines = file.readlines()

for line in lines:
    clean_line = line.strip()
    print(clean_line)

This is convenient when surrounding whitespace is not meaningful. However, strip() can remove meaningful indentation or spaces at the beginning or end of a line.

Preserving Leading Indentation

If leading spaces matter, use a narrower cleanup operation. rstrip() removes trailing whitespace while preserving leading whitespace:

for line in lines:
    clean_line = line.rstrip()
    print(repr(clean_line))

To remove only newline characters at the right side, use rstrip("\n"):

for line in lines:
    line_without_newline = line.rstrip("\n")
    print(repr(line_without_newline))

This preserves leading spaces and also preserves other trailing whitespace. On text read with universal newline handling, line endings are generally translated to \n. If you need to handle unusual line-ending details precisely, choose the file and newline settings deliberately.

MethodWhat it removesWhen to use itPotential concern
strip()Whitespace from both endsWhen indentation and surrounding spaces are not importantCan remove meaningful leading or trailing spaces
rstrip()Trailing whitespaceWhen leading indentation must remainAlso removes trailing spaces and tabs
rstrip("\n")Trailing newline charactersWhen only the line ending should be removedDoes not remove other line-ending or whitespace forms unless handled separately

Choose the Appropriate Reading Approach

readlines() loads every remaining line into memory at once. It is a good choice for reasonably sized text files when you specifically need a complete list, such as when you will access lines by index or process them in multiple passes.

For a large file, iterate directly over the file object instead of building a list:

with open("new_file.txt", encoding="utf-8") as file:
    for line_number, line in enumerate(file, start=1):
        clean_line = line.rstrip("\n")
        print(f"{line_number}: {clean_line}")

This still processes one line at a time, but it avoids storing all lines simultaneously. Choose based on the requirement: use readlines() when you need a list, and direct file iteration when sequential processing and lower memory use are more important.

ApproachReturn valueMemory behaviorBest use case
read()One string containing all remaining textLoads all text into memoryWhen the complete file content is needed as one string
readline()One string containing the next lineReads one line per callWhen code controls individual reads
readlines()A list of line stringsLoads all remaining lines into memoryWhen a complete list is required for later processing
Direct file iterationOne line string per loop iterationProcesses lines incrementallyLarge files or one-pass processing

Complete Example

with open("new_file.txt", "r", encoding="utf-8") as file:
    lines = file.readlines()

for number, line in enumerate(lines, start=1):
    clean_line = line.strip()
    print(f"{number}: {clean_line}")

This example opens the file safely, reads all lines into a list, closes the file automatically, numbers the list items starting at one, removes surrounding whitespace, and prints each cleaned line.

Troubleshooting

FileNotFoundError

This usually means the filename or relative path does not match the file's location. Check the current working directory, filename spelling, and extension. You can also provide a correct relative or absolute path.

Unexpected Blank Lines

If each string already ends with a newline, calling print(line) adds another newline. Use print(line, end=""), or clean the line before printing.

Spaces Disappear

If indentation or trailing spaces are meaningful, strip() may be too broad. Use rstrip("\n") to target the line ending, or use rstrip() when all trailing whitespace should be removed.

The Last Item Does Not Show \n

The final source line may not end with a newline character. The missing \n is normal file content behavior, not an error.

Large Files Use Too Much Memory

readlines() creates a list containing every remaining line. For large files, iterate directly over the file object so that lines can be processed incrementally.

UnicodeDecodeError

The selected encoding may not match the file's encoding. Specify the correct encoding in open(); encoding="utf-8" is a common choice for UTF-8 text files.

Run the Script

Save the Python code in a file such as read_lines.py, place new_file.txt where the script can find it, and run it from a terminal:

python read_lines.py