호그래머

Python 폴더 생성 (make directory) 23.01.24 본문

Python

Python 폴더 생성 (make directory) 23.01.24

호그래머 2023. 1. 24. 21:43
728x90

1. Python 폴더 생성하기

import os 필요

import os

def createFolder(basic_path,dir_name):
    try:
        if not os.path.exists(basic_path+"/"+dir_name):
            os.makedirs(basic_path+"/"+dir_name)
            print("폴더 생성 완료!")
        else:
            print("폴더가 이미 존재합니다!")
    except OSError:
        print ('폴더생성 에러 발생! ' +  basic_path+"/"+dir_name)

dir_path="C:/Users/82105/Desktop"
dir_name="테스트폴더"

createFolder(dir_path, dir_name)

결과

바탕화면에 "테스트폴더" 이름의 폴더가 생성 됩니다.

폴더가 존재하면 이미 존재한다는 메시지를 출력합니다.

 

1. 파이썬에서 쉽게 폴더를 생성 할 수 있습니다.

2. 폴더를 생성하기전 폴더 유무 파악을 하게됩니다.

3. 폴더를 생성할 경로와 폴더이름을 변수로 지정하였습니다.

728x90
Comments