Open In App

Python | os.mkdir() method

Last Updated : 16 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

All functions in the OS module raise OSError in the case of invalid or inaccessible file names and paths, or other arguments that have the correct type, but are not accepted by the operating system.

os.mkdir() method in Python is used to create a directory in Python or create a directory with Python named path with the specified numeric mode. This method raises FileExistsError if the directory to be created already exists.

os.mkdir() Syntax in Python

Syntax: os.mkdir(path, mode = 0o777, *, dir_fd = None)

Parameter:

  • path: A path-like object representing a file system path. A path-like object is either a string or bytes object representing a path.
  • mode (optional) : A Integer value representing mode of the directory to be created. If this parameter is omitted then default value Oo777 is used.
  • dir_fd (optional) : A file descriptor referring to a directory. The default value of this parameter is None. If the specified path is absolute then dir_fd is ignored.
  • Note: The ‘*’ in parameter list indicates that all following parameters (Here in our case ‘dir_fd’) are keyword-only parameters and they can be provided using their name, not as positional parameter.

Return Type: This method does not return any value.

os.mkdir() method Examples

There are various uses ways of os.mkdir() to create a directory in Python or create a directory with Python using os.mkdir, Here we are discussing some general examples of creating a directory in Python or creating a directory with Python those are following.

Create Directory In Python

In this example code uses os.mkdir() to Python create folder two directories: “GeeksForGeeks” and “ihritik” in the “/home/User/Documents” directory. The first directory is created with default permissions, while the second one is created with specified permissions (mode 0o666).

Python3




#Python create folder
# importing os module 
import os
   
# Directory
directory = "GeeksForGeeks"
   
# Parent Directory path
parent_dir = "/home/User/Documents"
   
# Path
path = os.path.join(parent_dir, directory)
   
# Create the directory
# 'GeeksForGeeks' in
# '/home / User / Documents'
os.mkdir(path)
print("Directory '%s' created" %directory)
   
   
# Directory
directory = "ihritik"
   
# Parent Directory path
parent_dir = "/home/User/Documents"
   
# mode
mode = 0o666
   
# Path
path = os.path.join(parent_dir, directory)
   
# Create the directory
# 'GeeksForGeeks' in
# '/home / User / Documents'
# with mode 0o666
os.mkdir(path, mode)
print("Directory '%s' created" %directory)


Output

Directory 'GeeksForGeeks' created
Directory 'ihritik' created



Errors while using os.mkdir() Method

In this example Python script uses `os.mkdir()` to create a directory named “GeeksForGeeks” in the “/home/User/Documents” path. If the directory already exists, a `FileExistsError` will be raised. If the specified path is invalid, a `FileNotFoundError` will be raised.

Python3




#Python create folder
# importing os module 
import os
   
# Directory
directory = "GeeksForGeeks"
   
# Parent Directory path
parent_dir = "/home/User/Documents"
   
# Path
path = os.path.join(parent_dir, directory)
   
# Create the directory
# 'GeeksForGeeks' in
# '/home / User / Documents'
os.mkdir(path)
print("Directory '%s' created" %directory)


Output

Traceback (most recent call last):
  File "osmkdir.py", line 17, in 
    os.mkdir(path)
FileExistsError: [Errno 17] File exists: '/home/User/Documents/GeeksForGeeks'



Handling Error while using os.mkdir() Method

In this example Python script attempts to create a directory named “GeeksForGeeks” in the “/home/User/Documents” path using `os.mkdir(). If the directory already exists, an `OSError` is caught, and the error message is printed.

Python3




# importing os module
import os
 
# path
path = '/home/User/Documents/GeeksForGeeks'
 
# Create the directory
# 'GeeksForGeeks' in
# '/home/User/Documents'
try:
    os.mkdir(path)
except OSError as error:
    print(error)   


Output

[Errno 17] File exists: '/home/User/Documents/GeeksForGeeks'



FAQ’s

What is the Difference between os.mkdir() and os.makedirs() ?

os.mkdir() is used to create a single directory, and it raises an error if the parent directory doesn’t exist.

os.makedirs() creates parent directories as needed, allowing the creation of nested directories, and it doesn’t raise an error if the directories already exist.

When should I use pathlib.Path.mkdir() vs os.mkdir() or os.makedirs()?

Use `pathlib.Path.mkdir()` when working with paths and directories, as it provides a more modern and object-oriented approach to file system operations.

`os.mkdir()` and `os.makedirs()` are suitable for basic directory creation, but `pathlib` is preferred for more advanced path manipulation and directory management.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads