How to Create Directory If it Does Not Exist using Python? Last Updated : 28 Nov, 2021 Comments Improve Suggest changes Like Article Like 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: Comment More infoAdvertise with us Next Article Python - How to Check if a file or directory exists P pulamolusaimohan Follow Improve Article Tags : Python Python directory-program Practice Tags : python Similar Reads R - Check if a Directory Exists and Create if It does not Directories and sub-directories are accessed by their corresponding paths in the R Programming Language. It is easy to work with these in R and perform operations related to the creation, copy, and movement of folders and sub-folders within the system. In this article, we will see how to check if a 2 min read Python - How to Check if a file or directory exists Sometimes it's necessary to verify whether a dictionary or file exists. This is because you might want to make sure the file is available before loading it, or you might want to prevent overwriting an already-existing file. In this tutorial, we will cover an important concept of file handling in Pyt 5 min read Python - How to Check if a file or directory exists Sometimes it's necessary to verify whether a dictionary or file exists. This is because you might want to make sure the file is available before loading it, or you might want to prevent overwriting an already-existing file. In this tutorial, we will cover an important concept of file handling in Pyt 5 min read How to create a duplicate file of an existing file using Python? In this article, we will discuss how to create a duplicate of the existing file in Python. Below are the source and destination folders, before creating the duplicate file in the destination folder. After a duplicate file has been created in the destination folder, it looks like the image below. For 5 min read How to Create a Folder if It Doesn't Exist in PHP ? We can easily create a folder in PHP, but before that, you have to check if the folder or directory already exists or not. So In this article, you will learn both to Check and Create a folder or directory in PHP. Methods: file_exists(): It is an inbuilt function that is used to check whether a file 3 min read How to create a list of files, folders, and subfolders in Excel using Python ? In this article, we will learn How to create a list of Files, Folders, and Sub Folders and then export them to Excel using Python. We will create a list of names and paths using a few folder traversing methods explained below and store them in an Excel sheet by either using openpyxl or pandas module 12 min read Like