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

Answer from Till on Stack Overflow
🌐
Python
docs.python.org › 3 › library › pathlib.html
pathlib — Object-oriented filesystem paths
Path.mkdir(mode=0o777, parents=False, exist_ok=False, *, parent_mode=None)¶
🌐
TradingCode
tradingcode.net › python › pathlib-path-mkdir-method
Make new directories with Python's `Path.mkdir()` method
Sometimes we first have to make that path, and then call the mkdir() method to create the directory. Python has several ways to make a path object. One option is to join path components with the overloaded / (division) operator.
People also ask

How can I check whether the path is a directory?
Use Path(path).is_dir(). Path.exists() also returns True for existing files, so it does not confirm the path is a directory.
🌐
askpython.com
askpython.com › python › examples › create-a-directory-in-python
Create a Directory in Python with pathlib - AskPython
Does opening a file create its parent directory?
No. Create the parent path first with Path(file_path).parent.mkdir(parents=True, exist_ok=True), then open the file.
🌐
askpython.com
askpython.com › python › examples › create-a-directory-in-python
Create a Directory in Python with pathlib - AskPython
Can I run a directory-creation script more than once?
Yes. Call Path.mkdir() with exist_ok=True when the target may already be a directory. A file at the target path still causes an error.
🌐
askpython.com
askpython.com › python › examples › create-a-directory-in-python
Create a Directory in Python with pathlib - AskPython
🌐
CodeGym
codegym.cc › java blog › learning python › python's mkdir -p equivalent: creating directories with p...
Python's mkdir -p Equivalent: Creating Directories with pathlib | CodeGym
June 25, 2026 - Both accept exist_ok=True; the semantics match. For new code in 2026, prefer Path.mkdir because it integrates with the rest of pathlib.
🌐
Python Engineer
python-engineer.com › posts › create-nested-directory
How to create a nested directory in Python - Python Engineer
August 7, 2021 - from pathlib import Path Path("/my/directory").mkdir(parents=True, exist_ok=True)
🌐
DEV Community
dev.to › hyperkai › pathmkdirparents-existok-in-python-3hi6
Path.mkdir(parents, exist_ok) in Python - DEV Community
October 14, 2024 - from pathlib import Path p = Path('dir3/dir3_1/dir3_1_1') p.mkdir(parents=True, exist_ok=True) # my_project # |-dir1 # | └-dir1_1 # |-dir2 # | └-dir2_1 # └-dir3 <- Here # └-dir3_1 <- Here # └-dir3_1_1 <- Here
🌐
PyTutorial
pytutorial.com › python-pathlib-mkdir-explained
PyTutorial | Python Pathlib mkdir() Explained
March 19, 2025 - To create a directory, first import the Path class from pathlib. Then, call mkdir() on a Path object.
Find elsewhere
🌐
Medium
martinxpn.medium.com › pathlib-the-oop-approach-of-working-with-file-system-in-python-65-100-days-of-python-7dbf85e9cec2
Pathlib — The OOP Approach of Working with File System in Python (65/100 Days of Python) | by Martin Mirakyan | Medium
April 10, 2023 - You can also use the mkdir() method to create multiple directories at once. from pathlib import Path # creating a path object path = Path('my_folder') # creating the directory path.mkdir() # creating multiple directories new_path = path / ...
🌐
AskPython
askpython.com › python › examples › create-a-directory-in-python
Create a Directory in Python with pathlib - AskPython
3 weeks ago - Use pathlib’s mkdir method to create a directory. Set parents=True when Python must create missing folders.
🌐
Miguendes
miguendes.me › python-pathlib
Python pathlib Cookbook: 57+ Examples to Master It (2022)
July 2, 2022 - The good news is that Path.mkdir() can handle situations like this well thanks to its parents argument. When parents is set to True, pathlib.mkdir creates the missing parent directories; this behavior is similar to the mkdir -p command in Unix ...
🌐
Python Guides
pythonguides.com › create-a-directory-in-python
How to Make a Directory in Python (os and pathlib)
1 week ago - If a regular file already has that name, Python still raises FileExistsError, because it can’t turn a file into a folder: from pathlib import Path target = Path("photos") print("before:", target.exists()) if not target.exists(): target.mkdir() print("after: ", target.is_dir()) clash = Path("photos.txt") clash.write_text("not a folder", encoding="utf-8") try: clash.mkdir(exist_ok=True) # exist_ok only ignores an existing *folder* except FileExistsError as err: print("FileExistsError:", err.strerror) clash.unlink()
🌐
Codemia
codemia.io › home › knowledge hub › mkdir -p functionality in python
mkdir -p functionality in Python | Codemia
September 23, 2025 - The mkdir -p functionality in Python is readily accessible using both os and pathlib modules, offering flexibility and ease of use. While os.makedirs() offers a straightforward procedural approach, pathlib provides an elegant object-oriented way of handling directories...
🌐
Better Stack
betterstack.com › community › questions › how-to-create-nested-directories-in-python
How can I safely create a nested directory in Python? | Better Stack Community
January 24, 2023 - from pathlib import Path p = Path('/root/directory/nested_directory') p.mkdir(parents = True, exist_ok = True)
🌐
OneUptime
oneuptime.com › home › blog › how to use pathlib for file paths in python
How to Use pathlib for File Paths in Python
January 27, 2026 - pathlib makes file path handling ... .read_text() and .write_text() for simple file operations · Use .mkdir(parents=True, exist_ok=True) for safe directory creation ·...
🌐
ZetCode
zetcode.com › python › pathlib
Python pathlib - working with files and directories in Python with pathlib
January 29, 2024 - We change the current working directory. Note that the directory is changed only inside the Python program. $ change_dir.py Current working directory: C:\Users\Jano\Documents\pyprogs\pathlib Current working directory: C:\Users\Jano\Documents\pyprogs · A new directory is created with mkdir.
🌐
DataCamp
datacamp.com › tutorial › comprehensive-tutorial-on-using-pathlib-in-python-for-file-system-manipulation
How to Use Python's Pathlib (with Examples) | DataCamp
May 22, 2024 - The introduction of the pathlib module in Python 3.4 has significantly simplified file system manipulation for developers. By providing an object-oriented approach to handling file paths, pathlib provides a structured and straightforward way to represent file system paths.
🌐
GeeksforGeeks
geeksforgeeks.org › python › create-a-directory-in-python
Create a Directory in Python - GeeksforGeeks
3 weeks ago - Import Path from the pathlib module. Create a Path object representing the target directory. Call mkdir() to create the folder.