본문 바로가기

PROGRAM/PyQt5 with Python

폴더 생성하기-기존 폴더가 있으면 다음 번호로

폴더 생성하기

- 폴더를 생성하는데 기존에 폴더가 존재하면 그다음 번호로 폴더 생성

예) capture_01, capture_02 폴더가 있으면 capture_03폴더 생성

import os
def make_folders(folder_path):
    num = 1
    while True:
        fname = folder_path + '_' + f'{num:02d}'
        if not os.path.exists(fname):
            os.makedirs(fname)
            break
        else:
            num += 1
            continue

if __name__ == '__main__':
    folder_path = 'captures'
    make_folders(folder_path)