EDElasticDomain

python snippet

Read a text file

Safely read a UTF-8 file with context managers.

from pathlib import Path
path = Path("data/input.txt")
# Read the whole file
text = path.read_text(encoding="utf-8")
print(text)
# Stream lines lazily
with path.open(encoding="utf-8") as fh:
for line in fh:
print(line.rstrip())