Create a directory in Python
Last Updated :
12 Jul, 2025
In Python, you can create directories to store and manage your data efficiently. This capability is particularly useful when building applications that require dynamic file handling, such as web scrapers, data processing scripts, or any application that generates output files.
Let's discuss different methods for creating a directory in Python.
Create a Directory using the OS
Module
The os
module provides a portable way of using operating system-dependent functionality. To create directories, you can use the os.mkdir()
and os.makedirs()
functions.
Create Folder in Python
To create a single directory, you can use the os.mkdir()
function.
Python
import os
# Specify the directory name
directory_name = "GFG"
# Create the directory
try:
os.mkdir(directory_name)
print(f"Directory '{directory_name}' created successfully.")
except FileExistsError:
print(f"Directory '{directory_name}' already exists.")
except PermissionError:
print(f"Permission denied: Unable to create '{directory_name}'.")
except Exception as e:
print(f"An error occurred: {e}")
Output

Creating Nested Directory
If you need to create nested directories, use the os.makedirs()
function.
Python
import os
# Specify the nested directory structure
nested_directory = "parent_folder/child_folder"
# Create nested directories
try:
os.makedirs(nested_directory)
print(f"Nested directories '{nested_directory}' created successfully.")
except FileExistsError:
print(f"One or more directories in '{nested_directory}' already exist.")
except PermissionError:
print(f"Permission denied: Unable to create '{nested_directory}'.")
except Exception as e:
print(f"An error occurred: {e}")
Output

Handling Errors
When creating directories, it's essential to handle potential errors, such as trying to create a directory that already exists or lacking permissions. Here are some common errors and how to handle them:
- FileExistsError: Raised when you attempt to create a directory that already exists.
- PermissionError: Raised when you lack the necessary permissions to create a directory in the specified location.
- Generic Exception: It’s good practice to catch any other exceptions that might occur.
Example of Handling Errors
Here’s an example that combines creating a directory and error handling:
Python
import os
# Specify the directory name
directory_name = 7
# Create the directory
try:
os.mkdir(directory_name)
print(f"Directory '{directory_name}' created successfully.")
except FileExistsError:
print(f"Directory '{directory_name}' already exists.")
except PermissionError:
print(f"Permission denied: Unable to create '{directory_name}'.")
except Exception as e:
print(f"An error occurred: {e}")
Output

Create a Directory using the Pathlib
Module
The pathlib
module offers a more object-oriented approach to filesystem paths and operations. It is available in Python 3.4 and later versions.
Creating a Directory
To create a directory using pathlib
, you can use the Path.mkdir()
method.
Python
from pathlib import Path
# Specify the directory name
directory_path = Path("new_folder_with_pathlib")
# Create the directory
try:
directory_path.mkdir()
print(f"Directory '{directory_path}' created successfully.")
except FileExistsError:
print(f"Directory '{directory_path}' already exists.")
except PermissionError:
print(f"Permission denied: Unable to create '{directory_path}'.")
except Exception as e:
print(f"An error occurred: {e}")
Output
Directory 'new_folder_with_pathlib' created successfully.
Creating Nested Directories
To create nested directories, you can use the parents
parameter with the Path.mkdir()
method.
Python
from pathlib import Path
# Specify the nested directory structure
nested_directory_path = Path("parent_folder/child_folder_with_pathlib")
# Create nested directories
nested_directory_path.mkdir(parents=True, exist_ok=True)
print(f"Nested directories '{nested_directory_path}' created successfully.")
Output

The parents=True
parameter ensures that any missing parent directories are also created, and exist_ok=True
prevents errors if the directory already exists.
Creating a Directory at a Specific Path
You can also create directories at a specific path by providing an absolute path or a relative path based on the current working directory.
Python
import os
# Specify the absolute path for the directory
specific_path = "/home"
# Create the directory
try:
os.mkdir(specific_path)
print(f"Directory '{specific_path}' created successfully.")
except FileExistsError:
print(f"Directory '{specific_path}' already exists.")
except PermissionError:
print(f"Permission denied: Unable to create '{specific_path}'.")
except Exception as e:
print(f"An error occurred: {e}")
Output

In this example, replace "/home/user/new_directory"
with your desired path. Ensure that the parent directories already exist, or use os.makedirs()
to create the entire directory structure.