Python 3 – os.mkdir() Method
When working with Python, you may come across situations where it becomes necessary to create a new directory. Fortunately, Python’s os
module provides us with a method called mkdir()
that allows us to create a new directory easily. In this article, we’ll discuss the os.mkdir()
method, including its syntax, parameters, and return value, and we’ll also take a look at some examples to help you get started.
The Syntax of os.mkdir()
The syntax of the os.mkdir()
method is quite simple. Here’s what it looks like:
os.mkdir(path, mode=0o777, *, dir_fd=None)
As you can see, the os.mkdir()
method takes in three parameters:
– path
– This is the path to the directory that you want to create.
– mode
(Optional) – This parameter specifies the permissions to use when creating the new directory. If it isn’t specified, then the default value is 0o777
, which grants all permissions to everyone.
– dir_fd
(Optional) – This is used to specify a file descriptor for the directory that you want to create. If it isn’t specified, then the default is None
.
Now that we have an understanding of the syntax, let’s take a closer look at each parameter.
Parameter 1 – path
The path
parameter is mandatory and is used to specify the path of the new directory that you want to create. This path can be either an absolute or a relative path.
If the path that you provide already exists and is a directory, the os.mkdir()
method will raise a FileExistsError
exception.
Parameter 2 – mode
The mode
parameter is optional and is used to specify the permissions to use when creating the new directory. You can specify these permissions using an octal number. Here are some of the most common values for the mode
parameter:
0o777
– This value grants read, write, and execute permissions to everyone.0o755
– This value grants read, write, and execute permissions to the owner and read and execute permissions to everyone else.0o700
– This value grants read, write, and execute permissions to only the owner of the file.
Parameter 3 – dir_fd
Finally, the dir_fd
parameter is optional and is used to specify a file descriptor for the directory that you are creating. If you don’t specify this parameter, then the new directory will be created in the current working directory.
The Return Value of os.mkdir()
The os.mkdir()
method doesn’t return anything. Instead, it simply creates a new directory at the specified path, with the specified permissions, and with the specified file descriptor.
Examples of the os.mkdir()
Method
Now that we have a better understanding of the os.mkdir()
method and its parameters, let’s take a look at some examples to see how it works in practice.
Example 1 – Creating a New Directory
The simplest use case for the os.mkdir()
method is to create a new directory. Here’s a sample code that creates a new directory called “new_directory” in the current working directory:
import os
os.mkdir("new_directory")
In this example, we import the os
module and then call the os.mkdir()
method with the path of the new directory that we want to create.
Example 2 – Creating a New Directory with Specific Permissions
If you want to create a new directory with specific permissions, you can use the mode
parameter to do so. Here’s a sample code that creates a new directory called “new_directory” in the current working directory with read and write permissions for only the owner:
import os
os.mkdir("new_directory", mode=0o600)
In this example, we set the mode
parameter to 0o600
, which grants read and write permissions to only the owner of the file.
Example 3 – Creating a New Directory with Specific File Descriptor
Finally, if you want to create a new directory with a specific file descriptor, you can use the dir_fd
parameter to do so. Here’s a sample code that creates a new directory called “new_directory” in the “/tmp” directory with a file descriptor of 3:
import os
dir_fd = os.open("/tmp", os.O_RDONLY)
os.mkdir("new_directory", dir_fd=dir_fd)
os.close(dir_fd)
In this example, we first open the “/tmp” directory and get a file descriptor of 3. Then we call the os.mkdir()
method with the dir_fd
parameter set to the file descriptor that weobtained from opening the directory. Finally, we close the file descriptor using the os.close()
method.
Conclusion
In Python, creating a new directory is easy thanks to the os.mkdir()
method. This method allows you to create a new directory with specific permissions and even with a specific file descriptor. In this article, we’ve covered the syntax, parameters, and return value of the os.mkdir()
method, as well as provided some examples to guide you through its usage. With this knowledge, you should be able to easily create new directories in your Python projects.