Create python directory Support if directory exists

An error will occur if the directory exists when the directory is created.

FileExistsError: [WinError 183]You cannot create a file that already exists.

How to check for existence

import os

DIR_NAME = 'dirname'
if not os.path.exists(DIR_NAME):
    os.mkdir(DIR_NAME)

How to handle exceptions

import os

DIR_NAME = 'dirname'
try:
    os.mkdir(DIR_NAME)
except OSError as ex:
    print(ex)

Recommended Posts

Create python directory Support if directory exists
Create Python folder Check if it already exists
Create a directory with python
Create Python module [CarSensor API support module csapi]
Check if the URL exists in Python
Golang mysql CREATE DATABASE IF NOT EXISTS
Python if statement
[Python] if statement
[Python] File / directory operations
[Python] Create multiple directories
Create SpatiaLite in Python
Python directory operation summary
Create a Python environment
Python basic if statement
[Python] If you suddenly want to create an inquiry form
[python] How to check if the Key exists in the dictionary
Create a Wox plugin (Python)
Create a function in Python
Create a dictionary in Python
Python "if __name__ == ‘__main__’: ”means
Create 3d gif with python3
Create gif video in Python
Create ToDo List [Python Django]
[Python] if __name__ == What is'__main__' :?
Create JIRA tickets using Python
Create a python numpy array
Presentation Support System with Python3
Get home directory with python
Let's create a Python directory structure that you won't regret later