How to Create a Directory in Python
If you want to create a new folder in Python, the safest beginner-friendly option is usually the standard library pathlib module.
In this guide, you will learn how to:
- Create a single directory in Python
- Create nested directories when parent folders do not exist
- Avoid errors when a directory already exists
- Choose between
pathlibandos.mkdir()/os.makedirs()
Quick answer
from pathlib import Path
Path("my_folder").mkdir(exist_ok=True)
This creates the folder if it does not exist. If it already exists, Python does nothing.
What this page helps you do
This page shows you how to:
- Create one folder
- Create several nested folders
- Avoid common folder-creation errors
- Pick the right tool for simple and nested paths
Best beginner option: pathlib.Path.mkdir()
For most beginner code, pathlib is the best choice because it makes paths easier to read and work with. If you are new to file paths, see working with file paths in Python.
Create one directory
from pathlib import Path
Path("my_folder").mkdir()
print("Folder created")
This creates a folder named my_folder in the current working directory.
Avoid an error if the folder already exists
If the folder might already be there, use exist_ok=True.
from pathlib import Path
Path("my_folder").mkdir(exist_ok=True)
print("Folder is ready")
This is useful when you want to make sure the folder exists without crashing your program.
Create nested directories
If parent folders do not exist yet, use parents=True.
from pathlib import Path
Path("projects/python/demo").mkdir(parents=True, exist_ok=True)
print("Nested folders created")
This can create:
projectsprojects/pythonprojects/python/demo
all in one step.
Using os.mkdir()
You can also create a directory with os.mkdir().
import os
os.mkdir("my_folder")
print("Folder created")
This works for a single folder, but there are limits:
- It creates only one directory
- It fails if the parent folder does not exist
- It fails if the folder already exists
Because of that, os.mkdir() is best for very simple cases. If you want a broader overview, see the Python os module overview.
Using os.makedirs() for nested folders
If you need to create nested folders with the os module, use os.makedirs().
import os
os.makedirs("a/b/c", exist_ok=True)
print("Nested folders created")
This creates all missing folders in the path.
Use this when:
- You need multiple levels of folders
- Some parent folders may not exist yet
- You want to avoid an error if the folders already exist
If you build paths from separate parts, you may also want to learn how os.path.join() works.
Common errors and how to avoid them
Here are the most common problems when creating a directory in Python.
FileExistsError
This happens when you try to create a folder that already exists.
Example:
from pathlib import Path
Path("my_folder").mkdir()
Path("my_folder").mkdir()
The second call raises an error.
Fix it by using:
from pathlib import Path
Path("my_folder").mkdir(exist_ok=True)
For more help, see FileExistsError in Python: causes and fixes.
PermissionError
This happens when Python does not have permission to write to that location.
Example:
from pathlib import Path
Path("/protected_folder").mkdir()
This may fail on systems where that location is protected.
Ways to fix it:
- Save the folder in your project directory instead
- Check that you have permission to write there
- Avoid protected system folders
For a full guide, see how to fix PermissionError: [Errno 13] Permission denied.
FileNotFoundError
This can happen when the parent path is invalid in some cases.
Example:
import os
os.mkdir("missing_parent/my_folder")
This fails because missing_parent does not exist.
Fix it by using nested-folder tools:
from pathlib import Path
Path("missing_parent/my_folder").mkdir(parents=True, exist_ok=True)
How to check that the directory was created
You can verify that the folder exists with pathlib.
Check whether the path exists
from pathlib import Path
folder = Path("my_folder")
print(folder.exists())
Expected output:
True
Check whether it is a directory
from pathlib import Path
folder = Path("my_folder")
print(folder.is_dir())
Expected output:
True
Print the full path for debugging
This helps when you are not sure where Python is creating the folder.
from pathlib import Path
folder = Path("my_folder")
print(folder.resolve())
You can also print the current working directory:
import os
print(os.getcwd())
If you need to check paths more generally, see how to check if a file exists in Python.
When to use each approach
Use the method that matches your task.
Use pathlib when possible
pathlib is usually the best option for beginners because:
- It is easy to read
- It works well with paths
- It supports both simple and nested directory creation
Examples:
from pathlib import Path
Path("one_folder").mkdir(exist_ok=True)
Path("a/b/c").mkdir(parents=True, exist_ok=True)
Use os.mkdir() for one simple folder
Choose os.mkdir() when:
- You only need one directory
- You know the parent folder already exists
- You do not mind handling errors yourself
Use os.makedirs() or Path.mkdir(parents=True) for nested folders
Choose one of these when:
- You need to create multiple folder levels
- Parent folders may not exist
- You want simpler setup code
Try to keep your path code consistent. If you start with pathlib, it is usually easiest to continue using pathlib.
Common mistakes
These are the most common causes of problems:
- Trying to create a directory that already exists
- Trying to create nested folders with
os.mkdir()instead ofos.makedirs() - Using a path where the program does not have permission to write
- Misspelling the target path
Helpful debugging commands:
from pathlib import Path
print(Path('my_folder').exists())
from pathlib import Path
print(Path('my_folder').is_dir())
import os
print(os.getcwd())
from pathlib import Path
print(Path('my_folder').resolve())
FAQ
How do I create a directory only if it does not exist?
Use either of these:
from pathlib import Path
Path("my_folder").mkdir(exist_ok=True)
import os
os.makedirs("my_folder", exist_ok=True)
How do I create nested directories in Python?
Use either of these:
from pathlib import Path
Path("a/b/c").mkdir(parents=True, exist_ok=True)
import os
os.makedirs("a/b/c", exist_ok=True)
Should I use pathlib or os?
For beginners, pathlib is usually easier to read and work with.
Why do I get PermissionError when creating a folder?
Your script may be trying to write in a protected location or a folder you do not own.