Create a New Directory in Python
To create a new directory in Python, we use the os.mkdir()
or os.makedirs()
function from the built-in os
module. The mkdir()
function creates a single directory, whereas makedirs()
can create nested directories. Additionally, Python 3.5+ provides pathlib.Path.mkdir()
for an object-oriented approach.
Examples to Create a New Directory
1. Creating a Single Directory Using os.mkdir()
In this example, we will create a new directory named my_folder
in the current working directory using the os.mkdir()
function.
main.py
import os
# Define the directory name
directory = "my_folder"
# Create the directory
os.mkdir(directory)
print(f"Directory '{directory}' created successfully!")
Explanation:
- Import the
os
module to interact with the file system. - Define the directory name as
directory = "my_folder"
. - Use
os.mkdir(directory)
to create the directory in the current working directory. - Print a success message confirming the directory creation.
Output:
Directory 'my_folder' created successfully!
2. Creating a Directory at a Specific Path
Here, we will create a new directory in a specified location instead of the current working directory.
main.py
import os
# Define the directory path
directory_path = "C:/Users/YourUsername/Documents/new_folder"
# Create the directory
os.mkdir(directory_path)
print(f"Directory created at: {directory_path}")
Explanation:
- Import the
os
module. - Define the full directory path using a string, e.g.,
directory_path = "C:/Users/YourUsername/Documents/new_folder"
. - Call
os.mkdir(directory_path)
to create the directory at the specified path. - Print a message indicating successful creation.
Output:
Directory created at: C:/Users/YourUsername/Documents/new_folder
3. Creating Nested Directories using os.makedirs()
To create multiple directories (a parent directory and its subdirectories) in one step, we use the os.makedirs()
function.
main.py
import os
# Define the nested directory structure
nested_directory = "parent_dir/sub_dir1/sub_dir2"
# Create nested directories
os.makedirs(nested_directory)
print(f"Nested directories created: {nested_directory}")
Explanation:
- Import the
os
module. - Define a nested directory structure as a string (e.g.,
parent_dir/sub_dir1/sub_dir2
). - Use
os.makedirs(nested_directory)
to create all directories in the path. - Print a success message after the directories are created.
Output:
Nested directories created: parent_dir/sub_dir1/sub_dir2
4. Creating a Directory using pathlib.Path.mkdir()
Starting from Python 3.5, we can use the pathlib
module for directory creation in an object-oriented way.
main.py
from pathlib import Path
# Define the directory path
dir_path = Path("new_directory")
# Create the directory
dir_path.mkdir()
print(f"Directory '{dir_path}' created successfully!")
Explanation:
- Import
Path
from thepathlib
module. - Define the directory path using
Path("new_directory")
. - Use
dir_path.mkdir()
to create the directory. - Print a success message indicating directory creation.
Output:
Directory 'new_directory' created successfully!
Conclusion
Python provides different ways to create directories:
os.mkdir()
: Creates a single directory.os.makedirs()
: Creates multiple nested directories.pathlib.Path.mkdir()
: Provides an object-oriented approach.
Use os.mkdir()
when creating a single folder, os.makedirs()
for nested folders, and pathlib
.