CodShot
Python Public

Read Text File Lines

Read a text file line by line and remove the newline characters.

#python #file #beginner #text
Python
from pathlib import Path

file_path = Path("notes.txt")

if not file_path.exists():
    print("File not found")
else:
    lines = file_path.read_text(encoding="utf-8").splitlines()

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

Notes

Pathlib makes file paths easier to read and works well on Windows, macOS, and Linux.

Snippet actions

CodShot user
CodShot user
Updated: 2026-06-03 15:31
Public snippets
0
Forks
CodShot AI Help
Ask about CodShot features, plans, workspace, AI limits, referrals, and public help topics.
Hi! I can help you understand how CodShot works. What would you like to know?
For account-specific or sensitive issues, please open a support ticket. Open support