Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
31 |
Tags
- 프로그래머스
- C++
- 컨테이너
- nvidia-smi
- YOLO
- Docker
- VS Code
- pip
- 핑거스타일
- C
- 채보
- 기타 연주
- paramiko
- SSH
- label
- mysql
- OpenCV
- 오류
- C#
- windows forms
- pandas
- error
- Selenium
- Linux
- JSON
- Visual Studio
- Numpy
- Python
- pytorch
- ubuntu
Archives
- Today
- Total
기계는 거짓말하지 않는다
Python 모든 하위 디렉터리 파일 경로들을 디렉터리 별로 묶기 본문
Python에서 하위 디렉터리들의 깊이를 알 수 없으며 각각 다른 깊이를 가지고 있고,
파일 경로들을 디렉터리 별로 묶고 싶을 경우 간단하게 사용할 수 있는 방법이다.
import os
def find_files_by_directory(directory, extensions):
files_by_directory = {} # dictionary에 저장
for root, dirs, files in os.walk(directory):
matched_files = [os.path.join(root, file) for file in files if file.endswith(extensions)]
if matched_files:
files_by_directory[root] = matched_files
return files_by_directory
if __name__ == "__main__":
directory = '.' # 시작 디렉터리(변경)
extensions = ('.jpg', '.png', '.jpeg', '.mp4', '.avi') # 찾을 파일 확장자
files_by_directory = find_files_by_directory(directory, extensions)
for dir_path, files in files_by_directory.items():
print(f"Directory: {dir_path}")
for file in files:
print(f" {file}")
dictionary의 key를 directory 경로를 이용하여 파일 경로 리스트를 저장한다.
'Python' 카테고리의 다른 글
Python logging 모듈 logger 설정(Settings) (0) | 2024.07.10 |
---|---|
Python 특정 프레임 수 이하 영상 데이터 삭제 (0) | 2024.06.30 |
Python pip를 이용하여 설치된 패키지(package) 업그레이드(upgrade) (0) | 2024.06.04 |
Python 함수 주석 설명 추가 (0) | 2024.05.22 |
Python 예외(Except) 처리 중 예외가 다시 발생 시 finally 절 실행 (0) | 2024.03.10 |
Comments