How to Create a Directory in Python?

To interact with the files or system, the “os” module is utilized in Python. The “os” module provides various functions to perform simple to complex operations such as creating, removing, appending, checking, etc files and directories in Python.  

Creating directories in Python is important because it will organize files and subfiles in a logical way. In this way, files can be organized in Python to process operations. To create a directory, the “os.mkdir()” and “os.makedirs()” functions are used in Python. 

This post provides different ways to create a directory in Python using the below contents:

Method 1: Using os.mkdir() Method

The “os.mkdir()” is used to create the directory in Python. The syntax of “os.mkdir()” is shown below:

os.mkdir(path, mode=0o777, *, dir_fd=None)

In the above syntax, the “path” parameter specifies the file path or location, and the “mode” parameter is used to allow and permit setting to the newly created directory. A parameter “dir_fd” specifies the file descriptor that is referred to as the directory For complete details regarding the “os.mkdir()” method, you can read this specific guide.

Example 1: Create a Directory in the Existing Location

The below code is used to create a directory in the existing location:

Code: 

import os
new_directory = "ILF"
os.mkdir(new_directory)
  • The module named “os” is imported.
  • The “os.mkdir()” takes the name of the directory and creates a new directory.
  • The directory is created in the current/present working directory of Python.

Output: 

The directory has been created.

Example 2: Create a Directory in the Specified Location

The below code is used to create the directory in the specified location:

Code: 

import os
new_directory = r"C:\Users\p\Downloads\itslinuxfoss\ILF"
os.mkdir(new_directory)
  • The “complete path” is assigned to a variable named “new_directory”.
  • The “os.mkdir()” function takes the complete path and creates the directory.

Output: 

The directory named “ILF” has been created at the specified location.

Example 3: Create a Directory With Permission

The following code is used to create the directory with permission mode:

Code: 

import os
new_directory = "ILF"
os.mkdir(new_directory, mode = 0o666)

The “os.mkdir()” function takes the directory name and mode “0o666” as an argument to create the directory with having read and write permission. The “0o666” means that anyone can read and write to the directory but not execute it.

Output: 

The directory has been created.

Example 4: Exception Handling With os.mkdir() Method

The exception occurs while creating the new directory in Python using the “os.mkdir()” function. Here is an example code that returns an error:

Code: 

import os
new_directory = "ILF"
os.mkdir(new_directory, mode = 0o666)

The “os.mkdir()” function is used to create the directory that already exists in the specified path.

Output: 

The error has occurred because the file already exists.

To resolve this error, the “if-else” statement is used along with the “os.path.isdir()” function. Here is an example:

Code: 

import os
new_directory = "ILF"
if os.path.isdir(new_directory) == False:
    os.mkdir(new_directory)
    print("Directory Created")
else:
    print("Directory Already Exists")
  • The “os.path.isdir()” function is used to check whether the specified directory is present/exists or not.
  • If the directory is present, the “os.mkdir()” function is utilized to create the directory.
  • But if the directory is not present, the “else” block is executed and displays the message that the ”Directory Already Exists”.

Output: 

Now, the exception has been turned into an output, i.e., “Directory Already Exists”.

Method 2: Using os.makedirs() Method

The “os.makedirs()” method is used to create a main directory along with the sub-directory. This method can create all intermediate-level directories. The syntax of this method is shown below:

os.makedirs(path, mode=0o777)

The above syntax parameter named “path” specifies the path of the file object. The optional parameter named “mode” denotes the mode of the newly created directory. (default value is set to ”0o777”).

Here is an example code:

Example 1: Create a Directory

To create a directory, the “os.makedirs()” function is used in the following code:

Code: 

import os
new_directory = "New_ILF"
os.makedirs(new_directory)

The “os.makedirs()” function takes the directory name as an argument to create the directory at the current working directory of Python.

Output: 

The directory named “New_ILF” has been created.

Example 2: Create a Main and Subdirectory Simultaneously

The below code is used to create the subdirectory and main directory at once using the “os.makedirs()” function:

Code: 

import os
new_directory = "New_ILF/example"
os.makedirs(new_directory)
  • The complete path of all the directory is assigned to a variable named “new_directory”.
  • The “os.makedirs()” take the complete path as an argument and create all the intermediate directories that are not created in the path.

Output: 

The main directory “New_ILF” has been created.

The sub-directory named “example” has been created inside the main directory.

Conclusion

To create a directory at the existing location or at the specified path with permission, the “os.mkdir()” and the “os.makedirs()” methods are used in Python. The “os.mkdir()” function takes the complete path or the name of the directory to create the directory at the existing path of the Python working directory or at the specified path. The “os.makedirs()” function can create a main directory along with the subdirectory at the specified path. 

This guide presented various ways to create a directory in Python using numerous examples.