How to Create a Directory in Python?

In this article, we will discuss how to create a directory in python.

Directory

The directory or simply say folder is a collection of files and sub-directory. A directory or folder is created to store our files in an organized manner.

Ways to create Directory in python

Using the Python os module we can easily work with directories. Let us see different ways to create a directory or folder in python.

  • Method 1-using os. mkdir()

First, let us discuss os. mkdir() function or method because this is the base of this whole concept.

os.mkdir()

This method is used to create a directory with a name path and a specified numeric mode. The default mode is 0777(octal). But we don’t need to focus on the mode part.

syntax:  os.mkdir(path[, mode])  or os.mkdir(path)

Here the path is the path which we need to be created. For example, C:\Users\HP\Desktop\python this is a path. Let us see how we can make a directory with this method.

For creating a directory with this method we have to give the name of our directory which we want to be created in the mkdir() method as an argument. In this way, our directory will be created in the current directory. Let us see this with the help of an example.

import os
print("Original directory Structure")
print(os.listdir())
#create new directory with name myDir
os.mkdir("myDir")
print("New directory structure")
print(os.listdir())

Output

Original directory Structure
['.vscode', '02_railway_oops.py', 'directory.py', 'file_handling.py', 'first.cpp', 'first.exe', 'hello.py', 'HelloWorld.class', 'newDir'] 
New directory structure
['.vscode', '02_railway_oops.py', 'directory.py', 'file_handling.py', 'first.cpp', 'first.exe', 'hello.py', 'HelloWorld.class', 'myDir', 'newDir']

Explanation:

In this example, we clearly see that first, we have no directory name “myDir” in the current directory but after we use os. mkdir() function our directory is created.

Note: If the directory already exists in the current directory then it will raise FileExistsError Error. To cope with this exception we can use try-except block.

  • Method 2-using os. makedirs()

First, let us discuss os. makedirs() function or method because this is the base of this whole concept.

os.makedirs()

This method is used to create a directory recursively with a name path and a specified numeric mode. But we don’t need to focus on the mode part.

For example, suppose we want to make a directory with this path /home/User/Documents/dir1/dir2 but in this path our directory Documents and dir1 not exist then it will not throw any error. It will create that missing directory also. The working will be the same as that of os. mkdir()

syntax:  os.makedirs(path[, mode])  or os.makedirs(path)

import os
print("Original directory Structure")
print(os.listdir())
#create new directory with name makeDir
os.makedirs("makeDir")
print("New directory structure")
print(os.listdir())

Output

Original directory Structure
['.vscode', '02_railway_oops.py', 'directory.py', 'file_handling.py', 'first.cpp', 'first.exe', 'hello.py', 'HelloWorld.class', 'myDir', 'newDir']
New directory structure
['.vscode', '02_railway_oops.py', 'directory.py', 'file_handling.py', 'first.cpp', 'first.exe', 'hello.py', 'HelloWorld.class', 'makeDir', 
'myDir', 'newDir']

So by these methods, we can create a directory in python.