Last Updated On By Anmol Lohana
There is a module in Python named OS that provides functions to interact with operating systems. It is one of Python’s standard utility modules. The module OS provides a portable way to use operating system-dependent functionality. The OS and os.path modules have many functions to deal with the file system or Python create directory. All functions that the OS module has will throw OSerror in an invalid file path and name or other correct arguments but not acceptable by the operating system.
Different methods are available in OS module: os.mkdir() and os.makedirs(). Making a single directory is done using the os.mkdir() method. This method will take the path as an argument. It cannot make subdirectories. For making multiple directories, we can make use of the os.makedirs() method. The path is optional in this method. We can create subdirectories using the os.makedirs() method. To use these methods, we need to import OS module first. Then use them to create directories. Let’s create some directories using these methods.
Table of Contents
We are going to create two directories named “AL” and “SM” inside the target directory “F:\MD” using the Python OS module with the mkdir() method.
import os
directory = "AL"
parent_dir = "F:\MD"
path = os.path.join(parent_dir, directory)
os.mkdir(path)
print("Directory '% s' created" % directory)
directory = "SM"
parent_dir = "F:\MD"
mode = 0o666
path = os.path.join(parent_dir, directory)
os.mkdir(path, mode)
print("Directory '% s' created" % directory)
Here, we can see that our directories “AL” and “SM” are created successfully.
We saw the method of creating directories using the Python OS module with the mkdir() method. Now, the question arises what if we try to make a directory that already exists? Well, the program will throw an error exception something like directory already exists: directory_name. Let’s have a look at it’s an example.
Here, we will try to create a directory that already exists at the location. Let’s see what it will give us in return.
import os
directory = "AL"
parent_dir = "F:\MD"
path = os.path.join(parent_dir, directory)
os.mkdir(path)
print("Directory '% s' created" % directory)
So, here we saw what it is returning, an exception error when we tried to create a directory that already exists. Does one more question arise after this that is what if you want to get a message instead of getting an exception error? Let’s have a look at an example.
We will try to get a message instead of getting an exception error. If the directory we are trying to create already exists.
import os
path = 'F:\MD\AL'
try:
os.mkdir(path)
except OSError as error:
print(error)
Here, we are successful in getting a message of directory existence instead of getting an exception error.
We are done with creating a single directory using the mkdir() method of the Python OS module. Now let’s go for creating multiple directories using the makedirs() method of the Python OS module.
Using the makedirs() method of the Python OS module, we will create multiple directories. Nested directories like directories inside directories.
import os
directory = "Anmol"
parent_dir = "F:/MD/A/Authors"
path = os.path.join(parent_dir, directory)
os.makedirs(path)
print("Directory '% s' created" % directory)
directory = "c"
parent_dir = "F:/MD/A/a/b"
mode = 0o666
path = os.path.join(parent_dir, directory)
os.makedirs(path, mode)
print("Directory '% s' created" % directory)
Our nested directories are created successfully.
F:\MD\A
F:\MD\A\a and F:\MD\A\Authors
F:\MD\A\a\b
F:\MD\A\a\b\c
F:\MD\A\Authors\Anmol
Directory already exists error as we saw in example no. 02.
import os
directory = "Anmol"
parent_dir = "F:/MD/A/Authors"
path = os.path.join(parent_dir, directory)
os.makedirs(path)
print("Directory '% s' created" % directory)
Getting message instead of an exception error as we did in example no. 03.
import os
directory = "Anmol"
parent_dir = "F:/MD/A/Authors"
path = os.path.join(parent_dir, directory)
try:
os.makedirs(path, exist_ok = True)
print("Directory '%s' created successfully" % directory)
except OSError as error:
print("Directory '%s' can not be created" % directory)
We are done with creating multiple directories using the makedirs() method of the Python OS module.
In conclusion, we saw Python create directory or how to create a directory using the Python OS module? We saw two methods of creating directories. First was the mkdir() method for creating a single directory. Second was makedirs() for creating multiple directories. Also, we saw examples of these two methods.