How to Create a Nested Directory in Python?

[toc]

Problem Statement: What is the most convenient approach to verify if a directory to which a file is going to be written already exists or not, and if not, how to create the nested directory using Python? 

Introducing the Problem

Sometimes you need to store/organize the data/information outside the current working script/program in files that can later be referred to by another application. You can either choose to build up another directory within the root directory itself in which you can save the files, or you can opt to create a subfolder/nested directory within the same working directory of your project and separately store the external files in that directory. Here’s a visual example in Pycharm-

In the above example, how would you check if directory “dir_a.1” exists or not? If not, how will you create it before storing the files in it?

Now that you have a clear picture of the problem at hand, let’s look at the different methods to safely create a nested directory.

Instruction: Consider that we have two directories:- “dir_A” and “dir_a.1” where “dir_A” is the parent directory and “dir_2” is the nested directory.

Method 1- Using pathlib.Path.mkdir

We can safely create a nested directory in Python 3.5 and above using pathlib.Path.mkdir.

To use mkdir() method of the pathlib module we first have to import Path from the pathlib module:

from pathlib import Path

Further, we need to create a Path object that includes the directory paths to be created and then link the object to the .mkdir() method to create the directory. Then we will simply call mkdir() method and set two parameters within it, i.e., parents and exist_ok.

When the directory Already exists then by default exist_OK is False and it raises FileExistsError if the given directory already exists. To avoid this error, you can set exist_OK to True.

from pathlib import Path
p = Path('/root/dir_A/dir_a.1')
p.mkdir(parents = True,  exist_ok = True)

Note: Path.mkdir returns the FileNotFoundError if the parent directory (in this case dir_A) is missing, i.e if the parent = False.

To avoid this error ensure that the parent folder (dir_A) is either present or set parents=True.

Caution: In the Python 3.4 version the pathlib module is missing the exist_ok option.

Method 2- Using os.makedirs

Another approach using which we create a nested directory in Python 3.2 versions and newer is by using the os modules makedirs method. You have to import the os module in your code to access system-specific functions.  

The os.makedirs() method constructs a directory recursively. It takes the path as an input and creates the missing intermediate directories. We can even use the os.makedirs method to create a folder inside an empty folder. In this case, the path to the folder you want to create will be the only single argument to os.makedirs().

Scenario I: When the parent directory and child directory does not exist.

import os
os.makedirs('./dir_A/dir_a.1')

Caution: You must always ensure that the path provided to the os.makedirs is the full path, i.e. the absolute path of the directory. It must not be the relative path or the program won’t run successfully.

Scenario 2: In case a directory already exists, the code given above will raise an exception as shown below.

To avoid this error we can use exception handling to along with the os.makedirs method. For this, we will use the try and except statements such that when the folder does not exist then the except block gets executed and the error is bypassed. The advantage of using try-except is that even when the directory does not exist, Python will create it.

import os

try:
    os.makedirs('./dir_A/dir_a.1')
except:
    print("Directory Already Exists!")

We will receive the following output if the directory already exists:

Directory Already Exists!

Bonus Read: One more condition that can occur is that if the directory gets created between the os.path.exists and the os.makedirs calls, the os.makedirs call fails and throws an OSError. However, even if you catch the OSError and proceed- it will overlook the failure to create the directory because of the other factors, such as insufficient permissions, full disk, etc. One solution to solve this is to use exception handling:

# Importing the os module
import os, errno
# Exception handling 
try:
    os.makedirs('/dir_A/dir_a.1')
except OSError as e:
    if e.errno != errno.EEXIST:
    	print(' The given directory already exists')

Note: Capturing the exception and using Errno is not that useful because the OSError: File exists, i.e. errno.EEXIST, is raised for both files and directories. Hence, it is advisable to simply check if the directory exists.

Method 3- Using distutils.dir_util

The distutils.dir_util module is used to operate on directories and trees of directories and, hence we can use this module to safely create a nested directory. We have to import the distutils.dir_util module before using it in our program. 

In the following program, we use the mkpath() method to check whether the directory exists or not. We will do nothing if the directory already exists. We can also raise the distutilsFileError if the program is unable to create the directory, i.e. subpath exists but is a file rather than a directory. 

# Import the distutils.dir_util module
import distutils.dir_util

distutils.dir_util.mkpath("./dir_A/dir_a.1")

Method 4 – The Conventional Way

Another approach to this problem is to take the conventional path. We will once again use the almighty os module.

  • In case the directories are not present, simply go ahead and check whether the directory is present or not using the os.path.isdir method.
  • If the directories are not present, go ahead and create them using os.mkdirs method.
  • If the directories are already present, do nothing.

Solution:

import os

# When directories are already present
if os.path.isdir("./dir_A/dir_a.1"):
    print("Directories Already Present!")
# When directories are not present
else:
    os.makedirs('./dir_A/dir_a.1')

Conclusion

We looked at different methods to safely create a nested directory in Python.  I hope it has helped to answer your queries. Please subscribe and stay tuned for more interesting articles in the future.

Recommended Read: How Do I List All Files of a Directory in Python?


Finxter Computer Science Academy

  • One of the most sought-after skills on Fiverr and Upwork is web scraping. Make no mistake: extracting data programmatically from websites is a critical life skill in today’s world that’s shaped by the web and remote work.
  • So, do you want to master the art of web scraping using Python’s BeautifulSoup?
  • If the answer is yes – this course will take you from beginner to expert in Web Scraping.