👩💻 도비는 공부중/💼 하계연구연수생(2023)
[추가업무] Python | 폴더 탐색 | 파일명 변경
오모짱_
2023. 7. 26. 15:50
1. Dataset download
>> 폴더 구조에 맞추어 파일 명 변경하기
2. os.walk(top, topdwon=True, onerror=-None, followlink=False)
top: 시작 dir의 경로
topdown: default = True
onerror: 순회 중 오류가 발생했을 때 호출할 함수 지정
followlinks: 심볼릭 링크 따라가지 않고 직접 순회
# 하위 디렉토리(Sub directory) 내의 *.csv 파일도 검색하여 리스트 만들 수 있도록 수정
def origin(pDir_path, pExt):
file_path_lists = []
file_name_lists = []
print(f'root: {pDir_path}')
for (path, dir, files) in os.walk(pDir_path):
print(f' path: {path}')
print(f' -dir: {dir}')
for filename in files:
full_filename = os.path.join(path, filename)
base = os.path.basename(full_filename)
base_include_path = os.path.abspath(full_filename)
ext = os.path.splitext(base)[1]
if ext == pExt:
file_name = os.path.splitext(base)[0] # file name
ext_name = os.path.splitext(base)[1] # extension name
file_path_lists.append(base_include_path)
file_name_lists.append(file_name)
return file_name_lists, file_path_lists
-> 현재 dir의 경로 (string)
-> 하위 dir 리스트
-> 현재 dir 에 있는 모든 파일 리스트 반환
☞ 매번 경로 입력하기 번거로움 -> 최상위 폴더에서 접근 가능하도록 수정
☞ 센서 별로 폴더 구조 다름 (CSV 폴더 고려 X)
☞ 같은 경로 내에서만 파일 명 변경 가능하도록
☞ 별도의 예외처리 고려 X