You could try:
p = pathlib.Path("temp/")
p.mkdir(parents=True, exist_ok=True)
fn = "test.txt" # I don't know what is your fn
filepath = p / fn
with filepath.open("w", encoding ="utf-8") as f:
f.write(result)
You shouldn’t give a string as path. It is your object filepath which has the method open.
source