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

</>
Copy
import os

# Define the directory name
directory = "my_folder"

# Create the directory
os.mkdir(directory)

print(f"Directory '{directory}' created successfully!")

Explanation:

  1. Import the os module to interact with the file system.
  2. Define the directory name as directory = "my_folder".
  3. Use os.mkdir(directory) to create the directory in the current working directory.
  4. 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

</>
Copy
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:

  1. Import the os module.
  2. Define the full directory path using a string, e.g., directory_path = "C:/Users/YourUsername/Documents/new_folder".
  3. Call os.mkdir(directory_path) to create the directory at the specified path.
  4. 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

</>
Copy
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:

  1. Import the os module.
  2. Define a nested directory structure as a string (e.g., parent_dir/sub_dir1/sub_dir2).
  3. Use os.makedirs(nested_directory) to create all directories in the path.
  4. 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

</>
Copy
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:

  1. Import Path from the pathlib module.
  2. Define the directory path using Path("new_directory").
  3. Use dir_path.mkdir() to create the directory.
  4. Print a success message indicating directory creation.

Output:

Directory 'new_directory' created successfully!

Conclusion

Python provides different ways to create directories:

  1. os.mkdir(): Creates a single directory.
  2. os.makedirs(): Creates multiple nested directories.
  3. pathlib.Path.mkdir(): Provides an object-oriented approach.

Use os.mkdir() when creating a single folder, os.makedirs() for nested folders, and pathlib.