We can use Path object in pathlib module to create a directory.

 

from pathlib import Path

p = Path('new_directory')
try:
    p.mkdir()
except FileExistsError as exc:
    print(exc)

 

In the mini program above, if a directory with the same name already exists, an error is raised. But, we can ignore this error by providing True to the parameter “exist_ok” in mkdir( ) method.

 

from pathlib import Path

p = Path('example_directory')
p.mkdir(exist_ok=True)