Code/Python

[Python] 지정 경로에 경로 존재 여부 확인

capaca 2023. 3. 3. 15:14

작성일자 : 23.02.06

import os

# 하위 디렉토리는 생성할 수 없음
os.mkdir('./new_folder')

# 디렉토리 생성 1
os.makedirs('./a/b/c', exist_ok=True) # 기존에 해당하는 폴더가 있어도 exception 에러 넘어갈 수 있음 exist_ok = True 하면 

# 디렉토리 생성 2
def makedirs(path): 
   try: 
        os.makedirs(path) 
   except OSError: 
       if not os.path.isdir(path): 
           raise
           
path = ~/test/
makedirs(path)