Daily Develope

[Python] 폴더 및 파일 탐색 본문

Develope/Python

[Python] 폴더 및 파일 탐색

noggame 2022. 4. 6. 09:22

ㅇ 폴더 및 파일 탐색

- 예) target_dir 에서 png, jpg 파일 출력 (non-recursion)

target_dir = f'{os.getcwd()}/sample'

for root, dirs, files in os.walk(target_dir):
    for file in files:
        if file.endswith(('.png', 'jpg')):
            print(file)

 

ㅇ 파일 존재 확인

import pathlib

if pathlib.Path('myfile.txt').exists():
	print("exists")

'Develope > Python' 카테고리의 다른 글

[Python] 정렬 Sort (Dict, Class)  (0) 2022.04.11
[Python] Kakao API 사용예시  (0) 2022.04.11
[Python] Numpy 넘파이 함수 정리  (0) 2022.04.04
[Python] MFCC 구현  (0) 2022.04.04
[Python] config 환경설정 관리 및 사용  (0) 2022.03.31
Comments