Function:
os.mkdir(path, mode=0o777, *, dir_fd=None)
Overview:
-
The function os.mkdir() creates a directory at the specified path in the filesystem.
-
Unlike the os.makedirs() function, the os.mkdir() creates only the leaf directory specified in a given path. The os.makedirs() creates all the directories specified in a path recursively.
-
When the leaf directory already exists in the filesystem the function mkdir() raises a FileExistsError.
-
The examples create directory using both absolute and relative paths and print the directory information using the os.stat() function.
Parameters:
path - The path of the directory to be created.
mode - Optional parameter specifying user, group and other permissions. Default value is 0o777.
dir_fd - Descriptor denoting the relative path of the directory to be created. Default value is None.
Return Value:
None
Exceptions:
FileNotFoundError - When the parent directory does not exist FileFileNotFoundError is raised.
FileExistsError - When the directory with the same name as the new directory to be created
exists a FileExistsError is raised.
Example 1 - Create a directory using absolute path:
# Example Python program to create a path = "/Ex/Notes" try: # Print information about the directory |
Output:
Directory information for /Ex/Notes: |
Example 2 - Create a directory using relative path and its directory descriptor:
# Example Python program that creates a dirPath = "/PythonProgs" # Get the directory descriptor of the path try: # Print info about the directory |
Output:
Directory info for /PythonProgs/Fonts: |