728x90
    반응형

     

    기능

    특정 조건을 만족하는 폴더 경로 모두 가져오기

     

    구현

    import os
    user_path = os.getcwd()
    list_path_hyperlink = []
    
    #user path 내의 폴더 경로 모두 가져오기
    only_one = True
    invalid_folder_path = ['(*&(&!@(#&']
    for (path, dir, files) in os.walk(user_path):
        satisfaction_folder=0
        for dirname in dir:
            str_path = "%s" % (path)
            str_dirname = "%s" % (dirname)
            str_file_path = "%s\%s" % (path, dirname)
    
            #특정 조건 (숫자숫자_)을 만족하는 폴더만 가져오기 // invalid_folder 변수를 사용하여 조건을 만족하지 않는 폴더 하위에 조건을 만족하는 폴더가 있더라도 찾지 않게끔 함.
            try:
                skip_flag=False
                for invalid_paths in invalid_folder_path:
                    if invalid_paths in str_file_path:
                        skip_flag=True
                        break
    
                if skip_flag == False:
                    if str_dirname[:2].isdigit() and str_dirname[2] == '_':
    
                        list_path_cutted.append(str_file_path.replace(user_path + '\\',''))  #사용자가 지정한 폴더 경로는 삭제하여 지정폴더 하위 폴더부터 분기 시작
                        list_path_hyperlink.append(str_file_path)
                        satisfaction_folder = satisfaction_folder + 1
                    else:
                        invalid_folder_path.append(str_file_path + '\\')
    
            except:
                pass
        if only_one == True and satisfaction_folder == 0:
            print('There are no satisfaction folders')
            print('Close program')
            exit()
            break
        only_one = False

     

    728x90
    반응형
    • 네이버 블러그 공유하기
    • 네이버 밴드에 공유하기
    • 페이스북 공유하기
    • 카카오스토리 공유하기