Zest.

Python: Read a file

A short, tested Python example for read a file. Copy it and adjust the names and paths for your project.

Python
with open("data.txt", encoding="utf-8") as f:
    text = f.read()          # whole file

with open("data.txt", encoding="utf-8") as f:
    for line in f:           # line by line
        print(line.rstrip())

Find it in Zest

Search for “python read a file” in Zest and this example appears above the results, with other matches to choose from. Searches stay private.

More Python examples

All Python examples