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
반응형
'배움 - 차근차근 기록하자 > [개발] 파이썬 🍕' 카테고리의 다른 글
[파이썬] 파일 경로에서 파일 이름만 리스트에 저장 (0) | 2022.04.21 |
---|---|
[파이썬] 리스트에서 중복 되는 폴더 경로 제거 (0) | 2022.04.20 |
[파이썬] 현재 실행중인 경로 얻기 (0) | 2022.04.18 |
[파이썬] 개념 06. 함수 (0) | 2021.08.16 |