Python:
Reading a text file

How to:

# Reading the whole file at once
with open('example.txt', 'r') as file:
    content = file.read()
    print(content)

# Reading line by line
with open('example.txt', 'r') as file:
    for line in file:
        print(line.strip())

Deep Dive

Reading text files is fundamental - and has been around since the early days of programming. Python’s straightforward open function has roots in the C standard library function fopen. Some alternatives for reading text files include using libraries such as pandas for CSVs or json for JSON files. Internally, when you read a file, Python asks the operating system to open a file stream, which is like a conveyor belt delivering data from the file to your program.

For large files, instead of read() which loads everything into memory, use readline() or iterate over the file object with a for loop to handle one line at a time – efficient and memory-friendly. While with open is the modern approach that automatically closes files, older scripts may use file.close() to do this manually, though it’s error-prone if exceptions happen before the close call.

See Also