Introduction to Python mkdir
In this article, we will explore the Python mkdir function, which serves the purpose of creating new directories. The mkdir command is a feature available in various scripting languages such as PHP and Unix. However, in older versions like DOS and OS/2, the mkdir command has been replaced with md. In Python, we can utilize the mkdir method, which is provided by the OS module for seamless interaction with operating systems. To create directories in Python, we employ the os.mkdir() method, specifying the numeric mode and path as arguments.
Working of mkdir in Python with Examples
The os.mkdir() method, obtained from the OS module, facilitates interaction with operating systems and provides access to OS-dependent functionalities. When attempting to create a directory that already exists, the os.mkdir() function raises a specific error called FileExistsError, indicating that the directory cannot be created because it already exists.
Syntax of mkdir() function:
os.mkdir(path, mode =0o777, *, dir_fd = none)
Parameters:
- The “path” parameter is used in the os.mkdir() function to specify the file system path for the directory to be created. It can be either a string or bytes object that represents a path-like object.
- The “mode” parameter is optional and is represented as an integer value. It determines the file permissions for the newly created directory. If this parameter is not specified, it defaults to a value of 0o777.
- The “*” symbol indicates that the following parameters are keyword-only parameters, meaning they can only be specified using their names.
- The “dir_fd” parameter is also optional and represents a file descriptor referring to the directory. Its default value is None.
This function os.mkdir() returns nothing, which means it cannot return any value.
Example #1
Example of mkdir() function.
Code:
import os
os.mkdir('sample')
print('The directory is created.')
Output:
In the above program, we can see a simple code for creating a directory using mkdir() function of the OS module in Python.
Example #2
Now let us see in detail how to create a directory with specifying path and mode while creating the directory. We will also see how to cross-check the creation of directories in the command prompt. Let us see the below example of the creation of directories.
Code:
import os
print("Python program to explain os.mkdir() method")
print("\n")
print("The directory name is given as:")
dir_name = "Educba"
print(dir_name)
print("\n")
print("The path to create directory specified is as follows:")
pa_dir = "D:/"
print("\n")
path = os.path.join(pa_dir, dir_name)
print("\n")
print("The mode is also specified as follows:")
mode = 0o666
print(mode)
print("\n")
os.mkdir(path, mode)
print("Directory has been '%s' created" %dir_name)
print("\n")
Output:
In the above program, we can see we are creating directories, one specifying the path and another directory specifying the mode. In the above screenshot of output, we can see both program and output of the program ad to confirm whether the directories are created; we can see it in the command prompt along with the date and time of creation.
Example #3
In Python, this mkdir() function, when used, might raise an error known as FileExistsError.
For the above program, if we try to execute it once again or if we are trying to create a directory that is already present in the drive, then it will give an error, and it can be shown as the below output.
Output:
In this article, we have observed that in Python, the os.mkdir() function is utilized for creating directories. The function accepts the directory path as an argument to determine the desired location for the directory. Python also offers the makedirs() function, which is similar to mkdir() but enables the creation of recursive directories. This means that all intermediate-level directories leading to the leaf directory will be created as well.
Example #4
Now let us demonstrate how to declare or use the Python makedirs() method along with syntax and the example below.
Syntax:
makedirs(path [,mode])
- path: This is used to specify the path to which the directory is created recursively.
- mode: This parameter is given to the directories to specify the mode of file permissions.
This function also will not return any value as the function mkdir() also does not return any value.
Code:
import os
print("Python program to explain os.mkdir() method")
print("\n")
dir_name = "Article"
print(dir_name)
print("\n")
dir_path = "D:/"
path = os.path.join(dir_path, dir_name)
os.makedirs(path)
print("Directory has been created using makedirs() '%s' created" %dir_name)
print("\n")
Output:
Conclusion
This article concludes that the mkdir() function uses the OS module to create directories in Python. The article also provides an example and syntax demonstrating the usage of this function. In this article, we also saw another function similar to mkdir() function for creating a recursive directory using makedirs().
Recommended Articles
This is a guide to Python mkdir. Here we discuss the introduction to Python mkdir and the working of mkdir with programming examples. You may also have a look at the following articles to learn more –