250x250
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 |
Tags
- Database
- glob
- MySQL
- decode()
- 오버라이딩
- 파이썬
- choice()
- __len__
- items()
- count()
- mro()
- fnmatch
- HTML
- __sub__
- shuffle()
- __getitem__
- zipfile
- CSS
- inplace()
- MySqlDB
- View
- __annotations__
- JS
- remove()
- randrange()
- shutil
- fileinput
- node.js
- locals()
- discard()
Archives
- Today
- Total
흰둥이는 코드를 짤 때 짖어 (왈!왈!왈!왈!왈!왈!왈!왈!왈!왈!왈!)
(파이썬, 과제) 사진파일 수정날짜 별로 분류하기 본문
728x90
반응형
과제
- 각 폴더에 담겨있는 사진들의 경로와 파일명 그리고 수정날짜를 얻어와 수정날짜의 월별로 구분하여 폴더에 담는다.
- 먼저 사진파일을 찾을 폴더에서 하위 폴더들의 경로를 얻어와 그 결로를 기반으로 사진파일들의 경로, 이름, 수정날짜를 얻고 엑셀파일에 담는다.
- 엑셀 파일에서 정보를 튜플로 받아와 리스트에 담는다.
- 리스트를 통하여 수정 날짜의 연도와 월을 얻어와 카테고리 리스트를 생성한다.
- 카테고리 리스트와 파일 리스트를 이용하려 카테고리별로 파일에 담는다.
파일 최초 상태
파일명 정리하기
In [4]:
import openpyxl as opx
import shutil
import os
import datetime
import pathlib
In [6]:
target_path = './과제사진'
In [30]:
def getFileName(target_path):
wb = opx.Workbook()
ws = wb.active
ws.cell(row=1, column=1).value = '파일경로'
ws.cell(row=1, column=2).value = '파일명(변경전)'
ws.cell(row=1, column=3).value = '수정날짜'
i = 2
filelist = []
folderlist = []
for foldername in os.listdir(target_path):
folderlist.append(target_path + '/' + foldername)
for current_dir in folderlist:
filelist = os.listdir(current_dir)
for filename in filelist:
ws.cell(row=i, column=1).value = current_dir + '/'
ws.cell(row=i, column=2).value = filename
ws.cell(row=i, column=3).value = fileCTime(current_dir, filename)
i = i + 1
wb.save(os.path.join(target_path, 'filelist.xlsx'))
In [38]:
def fileCTime(current_dir, filename):
filectime = os.path.getmtime(current_dir + '/' + filename)
return datetime.datetime.fromtimestamp(filectime).strftime('%Y-%m-%d')
In [41]:
getFileName(target_path)
파일 리스트 만들기
In [2]:
def excelRead(filepath):
wb = opx.load_workbook(filepath)
ws = wb.active
dirpath = [r[0].value for r in ws]
filename = [r[1].value for r in ws]
filectime = [r[2].value for r in ws]
len_num = len(dirpath)
datalist = []
for i in range(1, len_num):
temp_tuple = (dirpath[i], filename[i], filectime[i])
datalist.append(temp_tuple)
return datalist
In [7]:
file_list = excelRead(os.path.join(target_path, 'filelist.xlsx'))
print(file_list)
[('./과제사진/202208__/', 'IMG_2468.PNG', '2022-10-15'), ('./과제사진/202208__/', 'IMG_2469.PNG', '2022-11-11'), ('./과제사진/202208__/', 'IMG_2470.PNG', '2022-11-11'), ('./과제사진/202208__/', 'IMG_2471.JPG', '2022-09-24'), ('./과제사진/202208__/', 'IMG_2473.JPG', '2022-09-24'), ('./과제사진/202208__/', 'IMG_2474.JPG', '2022-09-24'), ('./과제사진/202208__/', 'IMG_2476.JPG', '2022-09-24'), ('./과제사진/202208__/', 'IMG_2478.JPG', '2022-08-31'), ('./과제사진/202208__/', 'IMG_2479.JPG', '2022-08-31'), ('./과제사진/202208__/', 'IMG_2480.JPG', '2022-08-31'), ('./과제사진/202208__/', 'IMG_2481.JPG', '2022-09-10'), ('./과제사진/202208__/', 'IMG_2482.JPG', '2022-09-10'), ('./과제사진/202208__/', 'IMG_2483.JPG', '2022-09-10'), ...]
폴더 생성하기
In [22]:
def categoryList(file_list):
filectime_list = []
for file in file_list:
temp_list = file[2].split('-')
filectime_list.append(temp_list[0] +'_'+ temp_list[1])
temp_set = set(filectime_list)
result = sorted(list(temp_set))
return result
In [25]:
categorylist = categoryList(file_list)
In [31]:
new_path = './사진정리'
def makeDir(new_path, categorylist):
for category in categorylist:
new_dir = pathlib.Path(os.path.join(new_path, category))
new_dir.mkdir(parents=True, exist_ok=True)
In [32]:
makeDir(new_path, categorylist)
파일 분류 및 이동하기
In [34]:
def moveFile(new_path, file_list, categorylist):
dirlist = os.listdir(new_path)
categorydict = {}
for file in file_list:
file_dir = file[0] + file[1]
temp_list = file[2].split('-')
categorydict[file_dir] = temp_list[0] +'_'+ temp_list[1]
for key, value in categorydict.items():
shutil.copy(key, new_path+'/'+value)
In [35]:
moveFile(new_path, file_list, categorylist)
실패
- 원래 계획은 수정 날짜가 아닌 사진 촬영 날짜를 얻어와 분류하려 하였다. 하지만 사진파일마다 찍은 날짜가 존재하는 파일도 있는 반면 정보가 아예 없는 사진 파일도 있어 데이터를 불러오는 중 에러발생가 발생한다. 아래와 같이 코드는 짜봤지만 이번 과제에서는 활용할 수 없어 코드로만 남겼다.
import from PIL import Image
def fileCTime(current_dir, filename):
file_dir = current_dir + '/' + filename
img = Image.open(file_dir)
meta_data = img._getexif()
make_time = meta_data[36867]
tump_time = make_time.split()
tump_list = tump_time.split(':')
result = tump_list[0] + '-' + tump_list[1] + '-' + tump_list[2]
return result
728x90
반응형
'과제' 카테고리의 다른 글
(HTML, 과제) 이력서 만들기 (0) | 2023.03.28 |
---|---|
(파이썬, MySQL, 과제) 자판기 프로그램 (0) | 2023.03.28 |
(파이썬, 과제) 파일 입출력 문제 (0) | 2023.03.13 |
(파이썬, 과제) 기초 문제 2 (0) | 2023.03.10 |
(파이썬, 과제) 기초 문제 1(BAEKJOON) (0) | 2023.03.10 |
Comments