15 lines
592 B
Python
15 lines
592 B
Python
import pathlib
|
|
|
|
from failure import Failure
|
|
|
|
|
|
def read_utf8(folder: str, name: str, consequence: str) -> str | Failure:
|
|
"""The text of the file name in folder; a Failure naming consequence where it cannot be read or is not UTF-8."""
|
|
try:
|
|
return pathlib.Path(folder, name).read_text(encoding="utf-8")
|
|
except OSError as failure:
|
|
return Failure(f"{name} in {folder}: {failure.strerror} - {consequence}.")
|
|
except UnicodeDecodeError as failure:
|
|
line = failure.object.count(b"\n", 0, failure.start) + 1
|
|
return Failure(f"{name} in {folder} is not UTF-8 on line {line} - {consequence}.")
|