APPLE 관련(Mac, iPad, iPhone, Applewatch)
[Mac/Pyhton] 폴더 내 파일 확장명에 맞춰 정리해주는 코드
ll___o___ll
2022. 10. 7. 21:00
반응형
Mac 기준입니다.
Python 을 통해 정리할 확장자를 정해주고, 거기에 맞는 파일들을 지정한 폴더로 옮겨주는 코드입니다.
여기에선 downloads에 담겨져 있는 파일들 중 jpg, png, gif, jpeg, heic 확장자 파일들을
images 폴더에 옮겨주는 코드입니다.
아래 코드를 응용하시면, hwp, xls, ppt, pdf 확장자를 가진 파일들을 다른 폴더로 옮길 수 있으시겠죠?
import os
# 정리할 확장자 리스트
extention_list = ['.jpg', '.png', '.gif', '.jpeg', 'heic']
# 정리할 폴더 , Mac 기준 ** 대신에 사용자 이름 입력
target = "/Users/**/Downloads"
# 만들 폴더
destination = target + "/images"
# 폴더가 없다면 만들기
if not os.path.exists(destination):
os.mkdir(destination)
# 현재 폴더 내 모든 파일 출력
file_list = os.listdir(target)
# 반목문을 통해 각 파일의 확장자를 확인
for file in file_list:
name, ext = os.path.splitext(file)
if ext in extention_list:
# 파일이동
source = os.path.join(target,file)
os.rename(source, os.path.join(destination, file))
반응형