There are different ways to create a nested directory depending on the versions of python you are using. For this example, we will create directories as shown in the image below.
Example 1: Using pathlib.Path.mkdir
For python 3.5 and above, you can use pathlib.Path.mkdir
to create a nested directory.
from pathlib import Path
Path("/root/dirA/dirB").mkdir(parents=True, exist_ok=True)
- Import class
Path
frompathlib
library. - Call the module
mkdir()
with two arguments parents andexist_ok
. - By default,
parents
is setFalse
. In this case, if the parent directory is not present, thenFileNotFoundError
is thrown. For example, if you want to create a nested directory/folder1/folder2/folder3
, andfolder1
(parent) does not exist already, thenFileNotFoundError
is raised by default. So, we set it toTrue
. exist_ok
isFalse
by default. If the directory already exists,FileExistsError
is raised. Set it toTrue
to prevent this error.
Note: You should provide the full path (absolute path) of the directory (not relative path). If the directory already exists, the above code does not raise an exception.
Example 2: Using os.makedirs
For python 3.2 and above, you can use os.makedirs
.
import os
os.makedirs("/root/dirA/dirB")
- Using method
makedirs()
from moduleos
, a nested directory can be created in a simple way. - The parameter passed is the nested directory we wanted to create.
You should provide the full path (absolute path) of the directory (not relative path). If the directory already exists, the above code does not raise an exception.
Example 3: Using distutils.dir_util
import distutils.dir_util
distutils.dir_util.mkpath("/root/dirA/dirB")
This example is also similar to Example 2. Here mkpath()
is used instead of makedirs()
.
You should provide the full path (absolute path) of the directory (not the relative path). If the directory already exists, the above code does not raise an exception.
Example 4: Raising an exception if directory already exists
import os
try:
os.makedirs("/dirA/dirB")
except FileExistsError:
print("File already exists")
This example is similar to Example 2.
- The statement is put inside the try block.
- If the directory is already present,
FileExistsError
is caught by the except block and runs the statements inside the block.
Also Read: