Python/분류가 애매함

Python에서 glob.glob() 쓸 때 디렉토리 주소 끝에를 /* 로 보정해주는 함수

공시탈출넘버원 2023. 9. 13. 16:08

import glob 이런거 빠졌는지 확인하고.

glob.glob(path_name) 쓸 때 path_name = '최상단폴더/하위폴더/하위폴더/.../파일있는폴더/*'로 써야한다.

그런데 주소 붙이다보면 '최상단폴더/하위폴더/하위폴더/.../파일있는폴더' 로 가져올 때가 있다.

언제 어디서 무엇이 틀렸는지 모르니까 그냥 함수로 보정해버리는게 편리하다.

 

def adjust_dir_ending(dir_folder_path):
    ## 보통 sorted(glob.glob(dir_folder_path))에서 dir_folder_path 가 /* 로 끝나는 str이 아닐 때 오류가 난다.
    ## 이거 설정하는거 자주 깜빡하므로, 폴더 주소를 /*로 정확히 끝내지 않았을 때 /* 를 붙여 보정해주는 함수
    ## 실제로 폴더 이름에는 / 또는 * 가 들어갈 수 없으므로, path는 문제없이 잘 보정된다.
	## input은 dir_folder_path 로 type str 이다.
    ## return은 dir_folder_path 로 type str 이다.
    
    if dir_folder_path[-2:] == '/*':
        pass
    elif dir_folder_path[-1] == '/':
        dir_folder_path = dir_folder_path + '*'
    elif dir_folder[-1] == '*':
        dir_folder_path = dir_folder_path[:-1] + '/*'
    else:
        dir_folder = dir_folder + '/*'

    return(dir_folder_path)