Software developers and system administrators need to routinely create directories to store files, data and sub directories. But not every directory has a parent directory. Sometimes you may need to create directory, whose parent (or ancestor) directories do not exist. There are several ways to create directory with missing parent directory. You can create such directory using programming languages such as Python, or using operating system commands in Windows, Linux & MacOS. In this article, we will learn how to create directory with missing parent directory, using either of these methods.
The Problem
Generally, we create a new directory, say, /home/ubuntu/data in a another directory /home/ubuntu using OS commands or programming languages. Since /home/ubuntu already exists, the new directory is easily created without any errors. But what if you want to create a directory /home/ubuntu/data/project/test. Here the parent directories /home/ubuntu/data and /home/ubuntu/data/project do not exist. So you will most likely get an error. In such cases, we need a solution that first creates these parent directories and lastly creates the innermost directory /home/ubuntu/data/project/test. This is the problem we are trying to solve.
How to Create Directory with Missing Parent Directory
Depending on your requirements, there are several simple ways to easily create a directory with missing parent directory.
1. Using Python
You can use programming languages like Python to create directories from within your script or application. It provides many useful libraries and functions for this purpose.
Python 3.5+
If you are using Python >=3.5, then using pathlib library will be very helpful. It provides a function mkdir() that recursively creates new directory and its parents. It does not throw an error if the directory or any of its parents already exist. It supports many options. Among them, the option parents=True ensures that the parent directories are also created, if not present already. The option exist_ok=True will not throw an error if the directory exists. Here is its syntax.
pathlib.Path('/path/to/directory').mkdir(parents=True, exist_ok=True)
Here is the code to create /home/ubuntu/data/project/test.
import pathlib
pathlib.Path('/home/ubuntu/data/project/test').mkdir(parents=True, exist_ok=True)
Python 3.2+
Python 3.4 comes with pathlib but it does not support exist_ok option, that disables error message in case the directory to be created already exists. Also, Python <3.4 does not support pathlib library. So, it is advisable to install a backport pathlib2 that not only supports mkdir() function but also exist_ok option. You can install it using the following command.
pip install pathlib2
OR
pip3 install pathlib2
Once it is installed, you can use mkdir() as shown in the previous section.
import pathlib2
pathlib.Path('/home/ubuntu/data/project/test').mkdir(parents=True, exist_ok=True)
Alternatively, you can also use os module that provides many functions to easily interact with file system. It provides makedirs() function to create directory along with its parent directories. Here is the command to create new directory.
os.makedirs(/path/to/directory, exist_ok=True)
Here is the command to create directory /home/ubuntu/data/project/test along with its missing parent directories.
import os
os.makedirs('/home/ubuntu/data/project/test', exist_ok=True)
Please note, only Python 3.2+ supports option exist_ok, with default value of False. Older Python versions do not support this option.
Python 2.7+
In Python 2.7+ also, you need to install pathlib2, which is a backport to pathlib, available in Python 3.5+. Thereafter, you can use mkdir() function in it.
import pathlib2
pathlib.Path('/home/ubuntu/data/project/test').mkdir(parents=True, exist_ok=True)
The makedirs() function in os module does not support exist_ok option and will throw an error if your new directory already exists. Therefore, we need to use it along with os.path.isdir() function, which basically checks if the directory exists or not. Here is the code for this purpose.
import os
try:
os.makedirs('/path/to/dir')
except OSError:
if not os.path.isdir('path/to/dir'):
raise
In the above code, we first call makedirs() function to create the directory. If it throws an error, we check if the directory exists using os.path.isdir() function. Typically, people use the following code for this purpose.
import os
if not os.path.isdir('/path/to/dir'):
os.makedirs('/path/to/dir')
The above code may create a race condition where the directory is created between the execution of os.path.isdir() and os,makedirs() functions. This happens in case of concurrent operations. It is to avoid such problems that we use the try…except method mentioned above.
Please note, you can also use os.path.exists() instead of os.path.isdir() function above. The first function checks if the said directory exists. If so, it returns True, else returns False. The second function checks if the path points to a directory. If so, it returns True, else returns False.
2. Using Linux/Unix/MacOS
mkdir is the most common command used to create directories in Linux/Unix and MacOS systems. It offers -p or –parent option, that automatically creates any missing parent directories. Also, it does not give any error if the directory to be created already exists. Here is its syntax.
mkdir -p /path/to/directory
Here is the command to create directory /home/ubuntu/data/project/test, along with its parent directories that do not exist.
mkdir -p /home/ubuntu/data/project/test
3. Using PowerShell
Windows PowerShell is a command line shell in Windows systems that allows you to manage tasks and Windows system. It provides New-Item cmdlet that allows you to create directories in Windows. Here is the syntax of command to create new directory.
New-Item -ItemType Directory -Path "C:\path\to\your\directory" -Force
In the above command, we use -ItemType option to specify that the new item to be created is a directory. We also use -Force option to disable error message in case the directory already exists. Here is the command to create directory c:\home\windows\data\project\test.
New-Item -ItemType Directory -Path "C:\home\windows\data\project\test" -Force
4. Using Windows Command Prompt
Like Linux, Windows also supports mkdir command. The only difference in this case is that you do not need to use -p or –parent option. Instead, you need to provide the full path to new directory. When you do this, mkdir will automatically create any missing parent directories. Here is its syntax.
mkdir "C:\path\to\your\directory"
Here is the command to create directory C:\home\win\data\project\test.
mkdir "C:\home\win\data\project\test"
Conclusion
In this article, we have learnt several easy ways to quickly create directory along with missing parent directories in your system. Depending on your requirement, you can do this programmatically or using OS commands. We learnt how to use os and pathlib modules to create directories using Python. We have also learnt how to do this for all operating systems – Windows, Linux and MacOS. We have also learnt how to suppress the error message, even if the directory to be created is already is already present. Depending on your requirement, you can use any of the above solutions.
FAQ
1. Can we use pathlib on all Python versions?
No. Pathlib library is available on Python 3.5+. For older Python versions, you need to install pathlib2, a backport to pathlib module.
2. How to Create directory with missing parents in Linux/Unix/MacOS?
Use mkdir with -p option, followed by path to directory to be created.
3. How to create directory with missing parents in Python?
For Python 3.5+, use pathlib.Path.mkdir() function. For older Python versions, use os.path.mkdir() function.
Also read:
Does Python Have Ternary Operator?
How to Read Large File in Python?
How to Execute System Command in Python?
Sreeram Sreenivasan is the Founder of Ubiq. He has helped many Fortune 500 companies in the areas of BI & software development.