소니카메라에서 추출한 파일을 백업하기 위해 바탕화면에 옮기다보면, arw, jpg, mov 파일이 뒤죽박죽 섞여있는데, 일일이 옮기기 귀찮은 경우가 있다. 그래서 과거에 확장자별로 폴더를 이동하는 스크립트를 짠 적이 있는데, 개인적으로 사용하기 위해 약간의 커스터마이징을 진행했다.
룰은, 기본적으로 MOV 폴더에 mp4, mov 등의 동영상 파일이 담기고, RAW파일에 arw 등의 raw 이미지 파일을 넣는다. jpg파일은 root 폴더에 두는 것이 개인적인 룰이다.
수정한 코드는 다음과 같다.
import os
import tkinter
from tkinter import filedialog
import shutil
# 폴더선택
root = tkinter.Tk()
root.withdraw()
dir_path = filedialog.askdirectory(
parent=root, initialdir="/", title='Please select a directory')
print("\ndir_path : ", dir_path)
# 폴더내파일검사
global_cache = {}
def cached_listdir(path):
res = global_cache.get(path)
if res is None:
res = os.listdir(path)
global_cache[path] = res
return res
def moveFile(ext, str):
# 현재 파일 위치
filePath = dir_path + '/' + item
# 옮길 파일 위치
finalPath = dir_path + '/' + str + '/' + item
if os.path.isfile(filePath):
shutil.move(filePath, finalPath)
if __name__ == '__main__':
# 기본 폴더 생성
newdir = ["RAW", "MOV"]
for nd in newdir :
newtDir = dir_path + '/' + nd
if not os.path.isdir(newtDir):
os.mkdir(newtDir)
# 각 기본 폴더에 옮겨질 확장자 리스트 정의
extList = {"RAW" : ["arw", "nef"],
"MOV" : ["mov", "mp4"],
}
for item in cached_listdir(dir_path):
# RAW에 해당하는 확장자인 경우
for extItem in extList["RAW"] :
if item.rpartition(".")[2] == extItem :
moveFile(extItem, "RAW")
# MOV에 해당하는 확장자인 경우
for extItem in extList["MOV"] :
if item.rpartition(".")[2] == extItem :
moveFile(extItem, "MOV")
print("complete")
즉흥적으로 만든거라 깊게 생각하지 않아 개발새발이지만, 다음에는 파일명을 생성한 날짜 기준으로 수정하는 스크립트를 추가해보려고 한다.
끝.
'Dead Code > Python' 카테고리의 다른 글
파이썬3.11.5, 포터블로 환경 설정하기 (0) | 2023.09.06 |
---|---|
[파이썬] 게시판에서 파일 이름 규칙에 따라 일괄 다운로드 (0) | 2023.06.23 |
티스토리 블로그 포스팅 제목 목록 txt파일 만들기 (1) | 2022.11.03 |
[파이썬] fatal error in launcher : unable to create process using... (0) | 2022.01.18 |
[파이썬] 확장자별로 폴더 생성, 파일 이동 (1) (0) | 2020.12.23 |
[파이썬] 초보자들의 단골메뉴 - while로 구구단 짜기 (0) | 2017.07.19 |
[파이썬] if 구문 연습, 가위바위보 짜기 (0) | 2017.07.17 |
[파이썬] 파이썬을 처음 공부하는 사람들을 위한 무료 eBOOK (0) | 2017.07.14 |