How to Create a Directory in Python?
Tags: python, programming, codenewbie, tutorial
Published: 2021-09-18 14:56:40

---
title: How to Create a Directory in Python?
published: true
date: 2021-09-17 12:59:59 UTC
tags: #python #programming #codenewbie #tutorial
canonical_url: https://itsmycode.com/how-to-create-a-directory-in-python/
---

ItsMyCode |

The  **`os `** module is a built-in utility available in both Python 2 and 3 versions, and it provides functions to interact easily with the operating system. The os and **`os.path`** modules provide various functions to interact with the file system.

Let’s take a look at various ways through which you can **create a directory in Python using the os module.**

## Method 1 – Using `os.mkdir()` function

The **`os.mkdir()`** method is used to create a directory in Python. This method will raise _ **FileExistsError ** _if the directory is already present in the specified path.

**Syntax: os.mkdir(path, mode = 0o777, \*, dir\_fd = None)**

**Parameters** :

**path: ** The location where you need to create a directory. The directory path will be a string object which includes the path and the directory name which needs to be created.

**mode (optional):** The permission that needs to be set to the newly-created directory. If you do not specify this parameter, then by default, it will set the permission as  **0o777**

**dir\_fd (optional):** A file descriptor referring to a directory. 

The default value will be set as None if you don’t provide any. If you specify the absolute path, then  **_dir\_fd _** is ignored.

**Return Value –**  The **`os.mkdir()`** doesn’t return any value.

### Example 1 – Create a directory in Python using os.mkdir()

The **`os.mkdir()`** creates the directory in the specified path if the directory not exists.

```python 
# Python program to create directory using os.mkdir() method

import os

# Directory path
dir_path = "C:/Projects/Tryouts/sample"
os.mkdir(dir_path)
print("Directory '% s' created" % dir_path)

# Directory path
dir_path2 = "C:/Projects/Tryouts/sample2"
# mode
mode = 0o666
os.mkdir(dir_path2, mode)
print("Directory '% s' created" % dir_path2)

```

**Output**

```python 
Directory 'C:/Projects/Tryouts/sample' created
Directory 'C:/Projects/Tryouts/sample2' created
```

### Example 2 – Exception if the directory already exists

The **`os.mkdir()`** method would raise a **_FileExistsError_** Exception if the directory in the location specified already exists.

```python 
# Python program to create directory using os.mkdir() method

import os

# Directory path
dir_path = "C:/Projects/Tryouts/sample"
os.mkdir(dir_path)
print("Directory '% s' created" % dir_path)

```

**Output**

```python 
Traceback (most recent call last):
  File "c:\Projects\Tryouts\main.py", line 7, in <module>
    os.mkdir(dir_path)
FileExistsError: [WinError 183] Cannot create a file when that file already exists: 'C:/Projects/Tryouts/sample'
```

## Method 2 – Using `os.makedirs()` method to create a nested directory in Python

The **`os.makedirs()`** method is used to create a directory recursively in Python, which means while making the leaf directory, if there are any intermediate directory is missed, the method **`os.makedirs()`** will create all of them.

**Syntax: os.makedirs(path, mode = 0o777, exist\_ok = False)**

**Parameters** :

**path: ** The location where you need to make a directory. It will be a string object which includes the path and the directory name which needs to be created.

**mode (optional):** The permission that needs to be set to the newly-created directory. If you do not specify this parameter, then by default, it will set the permission as  **0o777**

**exist\_ok (optional):** The default value is false, and if the directory exists, then **`os.makedir()`** will raise an FileExistsError. 

**Return Value –**  The `os.mkdir() `doesn’t return any value.

### Example 1 – Create a directory in Python using os.makedirs()

The **`os.makedirs()`** will create the nested directory if the parent directory doesn’t exist in the specified path.

```python 
# Python program to create directory using os.makedirs() method

import os

# Directory path
dir_path = "C:/Projects/Tryouts/test/sample/mydir"
os.makedirs(dir_path)
print("Directory '% s' created" % dir_path)

# Directory path
dir_path2 = "C:/Projects/Tryouts/test/sample/mydir2"
# mode
mode = 0o666
os.makedirs(dir_path2, mode)
print("Directory '% s' created" % dir_path2)

```

**Output**

```python 
Directory 'C:/Projects/Tryouts/test/sample/mydir' created
Directory 'C:/Projects/Tryouts/test/sample/mydir2' created
```

### Example 2 – Exception if the directory already exists

The **`os.makedirs()`** method would raise a **_FileExistsError_** Exception if the directory in the location specified already exists.

```python 
# Python program to create directory using os.makedirs() method

import os

# Directory path
dir_path = "C:/Projects/Tryouts/test/sample/mydir"
os.makedirs(dir_path)
print("Directory '% s' created" % dir_path)
```

**Output**

```python 
Traceback (most recent call last):
  File "c:\Projects\Tryouts\main.py", line 7, in <module>
    os.makedirs(dir_path)
  File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\os.py", line 225, in makedirs
    mkdir(name, mode)
FileExistsError: [WinError 183] Cannot create a file when that file already exists: 'C:/Projects/Tryouts/test/sample/mydir'
```

The post [How to Create a Directory in Python?](https://itsmycode.com/how-to-create-a-directory-in-python/) appeared first on [ItsMyCode](https://itsmycode.com).