Examples
- How to Create Directory in Python? New and If Not Exists
- How to Change Directory in Python? Change Current Working Directory
- How to Get Current Working Directory in Python?
- How to Remove Directory in Python? (Empty & With Files)
- How to Copy Directory in Python? Program With Examples
- How to Open Directory in Python? Program & Example
- How to Read All Files in Directory in Python? (All Types of Files)
- How to Copy File in Python From One Directory to Another? (With and Without shutil)
- Python File Append Program (Append to File)
- How to Read Text File in Python Line by Line? (3 Ways)
- How to Get File Name From File Path in Python?
- Get Number of Lines in File in Python (Line Count)
- How to Get Size of File in Python? (File Size Program)
How to Create Directory in Python? New and If Not Exists
Creating directories in Python is a common task when working with file systems or organizing files. Python provides several approaches to create directories, depending on different requirements.
In this post, we will explore two common scenarios: creating a new directory in Python and creating a directory if not exists.
The first scenario covers the basic process of how to create a new directory in Python. We will see how to achieve this using the os module and the pathlib module. These modules offer convenient functions and methods to interact with the operating system and handle file system operations.
The second scenario addresses a more specific need: creating a directory only if it doesn't already exist. This is useful when you want to ensure that a directory is created without accidentally overwriting existing data. We will again use the os module and the pathlib module to handle this scenario, and present different approaches to accomplish the task.
Create Directory in Python Using os module
To create a new directory in Python, you can use the os module:
Code
import os
# Specify the directory path
directory = "my_directory"
# Create the directory
os.makedirs(directory)
print("Directory created successfully!")
Output
Directory created successfully!
Explanation
In this program, the os.makedirs() function is used to create a new directory specified by the directory variable. The makedirs() function creates intermediate directories as needed, so you don't have to create parent directories separately.
After executing the os.makedirs() function, if the directory is successfully created, the program will print "Directory created successfully!".
Create Directory in Python Using pathlib module
Below is the program to create a new Python directory using the pathlib module:
Code
from pathlib import Path
# Specify the directory path
directory = "my_directory"
# Create the directory
path = Path(directory)
path.mkdir(parents=True, exist_ok=True)
print("Directory created successfully!")
Output
Directory created successfully!
Explanation
In this program, the Path.mkdir() method is used to create a new directory specified by the directory variable. The mkdir() method creates the directory, and the parents=True argument ensures that parent directories are created if they don't already exist. The exist_ok=True argument suppresses any errors if the directory already exists.
After executing the mkdir() method, if the directory is successfully created, the program will print "Directory created successfully!".
Create Directory if Not Exists (Using os module)
To create a directory in Python only if it doesn't already exist, you can use the os module:
Code
import os
# Specify the directory path
directory = "my_directory"
# Check if the directory already exists
if not os.path.exists(directory):
# Create the directory
os.makedirs(directory)
print("Directory created successfully!")
else:
print("Directory already exists!")
Output
Directory created successfully!
Explanation
In this program, the os.path.exists() function is used to check if the directory already exists. If the directory does not exist, the os.makedirs() function is used to create it. If the directory already exists, a message is printed indicating that the directory already exists.
Create Directory in Python if Not Exists (Using pathlib)
Another way of creating a directory in Python it it doesn’t exist already is by using the pathlib module:
Code
from pathlib import Path
# Specify the directory path
directory = "my_directory"
# Create the directory if it doesn't exist
path = Path(directory)
if not path.exists():
path.mkdir(parents=True)
print("Directory created successfully!")
else:
print("Directory already exists!")
Output
Directory already exists!
Explanation
In this program, the Path.exists() method is used to check if the directory already exists. If the directory does not exist, the Path.mkdir() method is used to create it. If the directory already exists, a message is printed indicating that the directory already exists.