The Wayback Machine - https://web.archive.org/web/20240624010923/https://www.geeksforgeeks.org/how-to-create-directory-if-it-does-not-exist-using-python/
Open In App

How to Create Directory If it Does Not Exist using Python?

Last Updated : 28 Nov, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, We will learn how to create a Directory if it Does Not Exist using Python.

Method 1: Using os.path.exists() and os.makedirs() methods

Under this method, we will use exists() method takes path of demo_folder as an argument and returns true if the directory exists and returns false if the directory doesn’t exist. makedirs() method is used to create demo_folder directory recursively .i.e. while creating demo_folder  if any intermediate-level directory is missing then it will create all those intermediate missing directories. in the above case if the geeks_dir is not present then it will first create geeks_dir then it will create demo_folder

Directory Used:

Python3




import os
  
# checking if the directory demo_folder 
# exist or not.
if not os.path.exists("path/to/demo_folder"):
      
    # if the demo_folder directory is not present 
    # then create it.
    os.makedirs("path/to/demo_folder")


Output:

Method 2: Using isdir() and makedirs()

In this method, we will use isdir() method takes path of demo_folder2 as an argument and returns true if the directory exists and return false if the directory doesn’t exist and makedirs() method is used to create demo_folder2 directory recursively .i.e. while creating demo_folder2  if any intermediate-level directory is missing then it will create all those intermediate missing directories. in the above case if the geeks_dir is not present then it will first create geeks_dir then it will create demo_folder2.

Directory Used:

Python3




import os
  
  
# checking if the directory demo_folder2 
# exist or not.
if not os.path.isdir("path/to/demo_folder2"):
    
    # if the demo_folder2 directory is 
    # not present then create it.
    os.makedirs("path/to/demo_folder2")


output:



Similar Reads

Create a directory in Python
The OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality. The os and os.path modules include many functions to interact with the file system. All functions in os module raise OSError in
6 min read
Python Program to Safely Create a Nested Directory
Creating a nested directory in Python involves ensuring that the directory is created safely, without overwriting existing directories or causing errors. To create a nested directory we can use methods like os.makedirs(), os.path.exists(), and Path.mkdir(). In this article, we will study how to safely create a Nested Directory in Python. Python Pro
2 min read
Rename all file names in your directory using Python
Given multiple files in a directory having different names, the task is to rename all those files in sorted order. We can use OS module in order to do this operation. The OS module in Python provides functions for interacting with the operating system and provides a portable way of using operating system-dependent functionality. We can go to the cu
1 min read
How to move all files from one directory to another using Python ?
In this article, we will see how to move all files from one directory to another directory using Python. In our day-to-day computer usage we generally copy or move files from one folder to other, now let's see how to move a file in Python: This can be done in two ways:Using os module.Using shutil module.Source and Destination Folder Setup Before Ru
2 min read
Delete a directory or file using Python
In this article, we will cover how to delete (remove) files and directories in Python. Python provides different methods and functions for removing files and directories. One can remove the file according to their need. Various methods provided by Python are - Using os.remove()Using os.rmdir()Using shutil.rmtree()Using pathlib.Path(empty_dir_path).
7 min read
Copy a directory recursively using Python (with examples)
Shutil module in Python provides many functions of high-level operations on files and collections of files. It comes under Python’s standard utility modules. This module helps in automating process of copying and removal of files and directories.shutil.copytree() method recursively copies an entire directory tree rooted at source (src) to the desti
5 min read
Get parent of current directory using Python
In Python, OS module is used to interact with the operating system. It comes under Python's standard utility modules. This module provides a portable way of using operating system dependent functionality. The *os* and *os.path* modules include many functions to interact with the file system. OS module provides various ways for getting the parent di
4 min read
Check if directory contains files using python
Finding if a directory is empty or not in Python can be achieved using the listdir() method of the os library. OS module in Python provides functions for interacting with the operating system. This module provides a portable way of using operating system dependent functionality. Syntax: os.listdir(<directory path>) Returns: A list of files pr
3 min read
Delete an entire directory tree using Python | shutil.rmtree() method
Shutil module in Python provides many functions of high-level operations on files and collections of files. It comes under Python’s standard utility modules. This module helps in automating the process of copying and removal of files and directories. shutil.rmtree() is used to delete an entire directory tree, path must point to a directory (but not
3 min read
Finding the largest file in a directory using Python
In this article, we will find the file having the largest size in a given directory using Python. We will check all files in the main directory and each of its subdirectories. Modules required: os: The os module in Python provides a way of using operating system dependent functionality. OS module is available with Python's Standard Library and does
2 min read
Practice Tags :
three90RightbarBannerImg