Python mkdir Function: Create Directories

In Python, the mkdir function is used to create directories. Here is how it is used:

os.mkdir(path, mode)

In this case, “path” is the path of the directory to be created, “mode” is an optional parameter used to specify the directory’s permissions, defaulting to 0o777.

For example, to create a directory named “new_directory” in the current working directory, you can use the following code:

import os

os.mkdir("new_directory")

Please note that if the specified directory already exists or if permission is not granted to create a directory, a FileExistsError or PermissionError exception will be raised.

bannerAds