Introduction to Python mkdir
In this article, we will see Python mkdir which is used for creating or making a new directory. This mkdir is a command in many different scripting languages like PHP, Unix, etc. Whereas in older versions like DOS, OS/2 also used such commands but mkdir is replaced with md in these OS. In Python, we can create a directory using mkdir which is a method in Python provided by the OS module for interacting with operating systems. In Python, it can be declared or defined as os.mkdir()method for creation of directories and we specify a numeric mode, path as its arguments.
Working of mkdir in Python with Examples
The os.mkdir() method which is obtained by an OS module for interaction with operating systems which provides a way of using OS-dependent functionality. There are different functions in this os module and any errors obtained are raised by OSError such as incorrect or inaccessible file names ad paths. In Python, os.mkdir() function is used for the creation of a new directory named path along with modes specified. This function raises an error known as FileExistsError when we are trying to create a directory which already exists.
Syntax of mkdir() function:
os.mkdir(path, mode =0o777, *, dir_fd = none)
- path: This parameter is used for specifying the file system path which is either a string or bytes object which represents a path-like object.
- mode: Which is also an optional parameter, and it is represented in integer value for directory creation and if this parameter is not specified then it will take a default value as 0o777.
- *: Is used to indicate the following parameters are keyword-only parameters with their names.
- dir_fd: This is also an optional parameter that is used as a file descriptor referring to the directory with having none as default value.
This function os.mkdir() returns nothing which means it cannot return any value.
Example of mkdir() function.
print('The directory is created.')
In the above program, we can see a simple code for creating directory using mkdir() function of the OS module in Python. In the above code, we can see that we have created a directory with the name “sample” and once it is done we print that directory is created.
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.
print("Python program to explain os.mkdir() method")
print("The directory name is given as:")
dir_name = "Educba"
print("The path to create directory specified is as follows:")
pa_dir = "D:/"
path = os.path.join(pa_dir, dir_name)
print("The mode is also specified as follows:")
mode = 0o666
print("Directory has been '%s' created" %dir_name)
In the above program, we can see we are creating directories one with specifying the path and another directory with 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 date and time of creation.
In Python, this mkdir() function when used it might raise an error known as FileExistsError.
The above program, if we try to execute once again or if we are trying to create directory which is already present in the drive then it will give error and it can be shown as below output.
In the above program, we are trying to create the same directory which was already created and it will give an error as a file already exists in the output. Therefore such errors can be handled by using OSError using try and except block.
In this article, we saw that to create directories in Python we use os. mkdir() function where we pass the directory path as an argument to specify where the directory must be located and mode is an optional argument where it specifies the file permission which is used while creating the directory. In Python, there is another function known as makedirs() which is similar to mkdir for the creation of directory but it is used for the creation of recursive directory which requires all intermediate-level directories to content leaf directory also.
Now let us demonstrate how to declare or use the Python makedirs() method along with syntax and example below.
In the above syntax the parameters can be elaborated as follows:
- 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.
print("Python program to explain os.mkdir() method")
dir_name = "Article"
dir_path = "D:/"
path = os.path.join(dir_path, dir_name)
print("Directory has been created using makedirs() '%s' created" %dir_name)
In the above program, we can see the directory article is also created.
In this article we conclude that mkdir() function is used for the creation of directories in Python it is provided by the OS module and it is declared as os.mkdir() method for the creation of directories in Python. We also saw the example and syntax in this article where we saw how to specify the path which is used for specifying the location of the directory to be created. In this article, we also saw another function similar to mkdir() function for creating a recursive directory using makedirs().
This is a guide to Python mkdir. Here we discuss the introduction to Python mkdir and working of mkdir with programming examples. You may also have a look at the following articles to learn more –