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.