How to create new directory in python

You can create a new directory in Python using the os module. Here are a few ways to do it:

Method 1: Using os.mkdir()

import os

directory_name = "my_new_directory"
os.mkdir(directory_name)

This will create a new directory with the specified name in the current working directory.

Method 2: Using os.makedirs()

import os

directory_name = "my_new_directory"
os.makedirs(directory_name)

This will create a new directory with the specified name in the current working directory, and all the necessary parent directories will be created if they don't exist.

Method 3: Using pathlib module (Python 3.4 and later)

import pathlib

directory_name = "my_new_directory"
pathlib.Path(directory_name).mkdir(parents=True, exist_ok=True)

This will create a new directory with the specified name in the current working directory, and all the necessary parent directories will be created if they don't exist. The exist_ok=True parameter will prevent an error if the directory already exists.

Method 4: Using shutil module (Python 3.2 and later)

import shutil

directory_name = "my_new_directory"
shutil.rmtree(directory_name, ignore_errors=True)
shutil.rmtree(directory_name, ignore_errors=True)

This will create a new directory with the specified name in the current working directory, and all the necessary parent directories will be created if they don't exist. The ignore_errors=True parameter will prevent an error if the directory already exists.

Note: Make sure to replace directory_name with the actual name you want to give to your new directory.