Create Python folder Check if it already exists

background

There is a lot of programming code for creating folders in Python, Make a memorandum just in case.

Sample code

You can create folders, but If the folder already exists, you will get an error. Use if to avoid it.

make_folder.py


import os

new_path = "demo_folder"#Folder name
if not os.path.exists(new_path):#If there is no directory
    os.mkdir(new_path)#Create the folder name you want to create

result

The first time you run it, the folder will be created. When I run it a second time, nothing happens because the folder has already been created.

Recommended Posts

Create Python folder Check if it already exists
Create python directory Support if directory exists
Check if the URL exists in Python
[python] How to check if the Key exists in the dictionary
Code Python to check and graph if it follows Benford's law
Golang mysql CREATE DATABASE IF NOT EXISTS
Check if it was run as root
Check if the characters are similar in Python
Python> Run-time argument> Check if -d is attached
In bash, "Delete the file if it exists".
Python if statement
Create a python script to check if the link at the specified URL is valid 2
Python nan check
python grammar check
Create a python script to check if the link at the specified URL is valid
[Python] if statement
Check if the string is a number in python
Check if the expected column exists in Pandas DataFrame
Check if it is Unix in the scripting language
python> check NoneType or not> if a == None:> if a is None:
I wondered if Python 3.4 was faster, but it was slower
Check if it is Unix in the scripting language