我通过提供路径作为PosixPath打开文件。
from pathlib import Path
SOURCE_DIR = Path(__file__).resolve().parent.parent.parent
ROOT_DIR = SOURCE_DIR.parent
DATA_DIR = ROOT_DIR / "data"
with open(DATA_DIR / "filename.txt", "r") as f:
VALUES = [line.strip() for line in f.readlines()]
我收到以下错误:TypeError: coercing to Unicode: need string or buffer, PosixPath found
。
如何将整个路径转换为字符串(从PosixPath)或打开PosixPath文件?