# 현재 드라이브에서 지정한 디렉터리를 모두 탐색 예제
import os
# 현재 드라이브의 루트 경로를 가져옴
drive = os.getenv("SystemDrive")
# 현재 드라이브에 temp 디렉터리 결합
root_path = drive + "\\temp"
# 파일 경로를 저장할 리스트를 생성
file_list = []
# 지정한 디렉티리의 모든 파일을 검색
for dirpath, dirnames, filenames in os.walk(root_path):
for filename in filenames:
file_path = os.path.join(dirpath, filename)
file_list.append(file_path.split(os.sep))
# 파일 경로 리스트를 출력
for file_path in file_list:
print(file_path)
[실행 결과]
['C:', 'temp', 'kisoo.pem']
['C:', 'temp', 'log.txt']
['C:', 'temp', 'AUtempR', 'amoncdw567.sys']
['C:', 'temp', 'data', 'my1.json']
['C:', 'temp', 'data', 'my2.json']
['C:', 'temp', 'textfiles', 'test1.txt']
['C:', 'temp', 'textfiles', 'test2.txt']
반응형
'Python 기초' 카테고리의 다른 글
파이썬, 부동소숫점 계산 정확도 보장하기 (0) | 2023.06.26 |
---|---|
사전 객체를 문자열로 바꾸거나 그 문자열을 사전 데이터로 변환하는 다섯 가지 예제 (0) | 2023.04.12 |
파이썬, 멀티스레딩, 멀티프로세싱 예제 (백그라운드 실행) (0) | 2023.03.23 |
파이썬, pip install 캐시 무시하고 설치하기 (0) | 2023.03.11 |
파이썬, 이진수 문자열 다양한 출력 예제 (0) | 2023.02.15 |