Python Create Directory | os.mkdir() Method

In many programming languages, the “mkdir” command is used to create a new directory. In Python, the OS module interacts with the operating system and provides different functions like creating and deleting file directories. The “mkdir” method is used to create the directory by declaring “os.mkdir()” in Python script with its “path name” and “numeric mode” value inside the argument. We can create temporary and permanent directories using a different module in Python code.

This write-up will explain a detailed understanding of the Python create directory method “os.mkdir()”. The outcomes are as follows:

So let’s get started!

What is the os.mkdir() Method?

The “os.mkdir()” method creates the new directory file in Python. This method is obtained from the os module which has various functions like creating or deleting a directory. The “os.mkdir()” method shows an error when the directory file already exists in the same location.

Top 10 Linux Distros

The syntax of “os.mkdir()” is shown below:

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

In the above syntax:

  • Path”: Specifies the location of a new directory. The value can be any bytes object or string.
  • Mode”: Allows or permits setting the new directory. This value will be an integer and not necessary to define. The default value of “Mode” is “0o777”.
  • dir_fd”: A file descriptor referred to as the directory. The “dir_fd” parameter default value is “None”.

Let’s see how the os.mkdir() method works:

Example 1: How to Create a Directory Using os.mkdir() Method?

The “os.mkdir()” method creates a new directory by only specifying the path name inside the parentheses. The “mode” value and other parameters are optional to define inside the “os.mkdir()” method. Let’s understand the concept by following code:

Code

# Create directory using os.mkdir() method
import os
directory_path = "F:\itslinuxfoss\Documents/newfile"
os.mkdir(directory_path)
print("The Directory '% s' created" % directory_path)

In the above code:

  • Firstly, “os module” is imported in the program.
  • Secondly, the path name is specified inside the quotation marks and saved in a variable named “directory_path”.
  • The “os.mkdir()” method takes the path variable inside the parenthesis and creates a new directory named “new file

Note: The “/” forward slash used before the file named “newfile” while defining the path is mandatory to use to create a new Directory.

Output

The above console output shows that a new directory is created.

In the above snippet, the directory named “newfile” is created.

Example 2: How to Handle Directory Already Exists Error Using os.mkdir() Method?

Sometimes, when we create a new directory, an error like “file name already exists” or “file name error” occurs in the program. These errors are resolved by creating a new file with a different directory name or removing duplicates of the existing one. Let’s see an example below where this error is handled and removed using the “try-except” block.

Code

import os
directory_path = 'F:\itslinuxfoss\Documents/newfile'

try:
    os.mkdir(directory_path)
except OSError as error:
    print('File already exist')
    print('creating new path')
    new_path = 'F:\itslinuxfoss\Documents/newfile1'
    os.mkdir(new_path)

In the above code:

  • The “os module” is imported.
  • The directory file name and location is specified and stored in a variable named “directory_path”.
  • The “try-except” block is used in the above code to handle the error.
  • Try” block creates the directory if it is new and not present in the directory path.
  • Except” block runs when the file already exists in the specified path. A new path is defined in the “except” block with a new directory name.

Note: The “/” forward slash used before the file named “newfile” while defining the path is mandatory to use to create a new Directory.

Output

In the above output, a new directory is created by changing the name.

The above snippet shows that the new directory named “newfile1” is created.

That’s all from this Python tutorial.

Conclusion

In Python, the “os.mkdir()” method is used to create new directories. The “os module” in Python provides functionality in which the python script can interact with the operating system and perform some operations, like creating a new directory or deleting the directory. The “already file exists error” can also be removed with the help of the “try-except” block and the “os.mkdir()” method.

This tutorial has provided a detailed understanding of the “os.mkdir()” method with numerous examples.