-- Load mathjax -->
-- MathJax configuration -->
-- End of mathjax configuration -->
1):
import os
os.getcwd()
꺼짐(1):
'C:\\LeeCoding\\Python\\Jupyter'
2)에서:
# 정리 대상 폴더 경로를 설정
target_path="./고라니"
3시에):
# 압축 파일 확인
import glob
zipfile_path = ()
for filename in glob.glob(os.path.join(target_path, '**/*.zip'), recursive=True):
zipfile_path.append(filename)
print(zipfile_path)
('./고라니\\데이터저장_물류.zip')
(4)에서:
# 압축 파일 해제
import zipfile
for filename in zipfile_path:
with zipfile.ZipFile(filename) as myzip:
zipinfo = myzip.infolist() # zip파일 정보
for info in zipinfo:
decode_name = filename.encode('cp437').decode('euo-kr') # 한글 때짐 방지
info.filename = os.path.join(target_path, decode_name)
myzip.extract(info)
(<ZipInfo filename="D_20220110_╡Ñ└╠┼═└·└σ_╣░╖∙_001.pdf" compress_type=deflate external_attr=0x20 file_size=13264 compress_size=12312>, <ZipInfo filename="D_20220110_╡Ñ└╠┼═└·└σ_╣░╖∙_002.pdf" compress_type=deflate external_attr=0x20 file_size=13264 compress_size=12312>, <ZipInfo filename="D_20220723_╡Ñ└╠┼═└·└σ_╣░╖∙_001.pdf" compress_type=deflate external_attr=0x20 file_size=13264 compress_size=12312>, <ZipInfo filename="D_20220723_╝╛┼═░í╡┐╟÷╚▓_╣░╖∙_002.pdf" compress_type=deflate external_attr=0x20 file_size=13264 compress_size=12312>)
2. 파일명 정리
(7)에서:
!
pip install openpyxl
Collecting openpyxl
Downloading openpyxl-3.1.2-py2.py3-none-any.whl (249 kB)
Collecting et-xmlfile
Downloading et_xmlfile-1.1.0-py3-none-any.whl (4.7 kB)
Installing collected packages: et-xmlfile, openpyxl
Successfully installed et-xmlfile-1.1.0 openpyxl-3.1.2
WARNING: You are using pip version 20.2.1; however, version 23.0.1 is available.
You should consider upgrading via the 'c:\users\acer\appdata\local\programs\python\python38\python.exe -m pip install --upgrade pip' command.
8시에):
import openpyxl as opx
(21)에서:
# 폴더별 파일명을 입력 받아 액셀파일에 저장하는 함수
def getFileName(target_path):
wb = opx.Workbook()
ws = wb.active # 새로 생성한 WorkBook의 활성화 시트를 ws로 정의
ws.cell(row=1, column=1).value="파일경로"
ws.cell(row=1, column=2).value="파일명(변경전)"
ws.cell(row=1, column=3).value="파일명(변경후)"
i = 2
current_dir = target_path
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
i = i + 1
wb.save(os.path.join(target_path, 'filelist.xlsx'))
(22)에서:
getFileName(target_path)
3. 파일 이름 바꾸기
(23)에서:
def excelRead(filepath : str) -> list:
wb = opx.load_workbook(filepath)
ws = wb.active
dirpath = (r(0).value for r in ws)
file_before = (r(1).value for r in ws)
file_after = (r(2).value for r in ws)
len_num = len(dirpath)
datalist = ()
for i in range(1, len_num):
temp_tuple = (dirpath(i), file_before(i), file_after(i))
datalist.append(temp_tuple)
return datalist
(24)에서:
rename_list = excelRead(os.path.join(target_path, 'filelist.xlsx'))
print(rename_list)
(('./고라니/', 'A_2022_01_13_부서로그_인사_001.pdf', 'A_2022_01_13_부서로그_인사_001.pdf'), ('./고라니/', 'A_2022_01_13_부서로그_인사_002.pdf', 'A_2022_01_13_부서로그_인사_002.pdf'), ('./고라니/', 'A_2022_01_13_부서로그_인사_003.pdf', 'A_2022_01_13_부서로그_인사_003.pdf'), ('./고라니/', 'A_2022_04_10_생산로그_생산_001.pdf', 'A_2022_04_10_생산로그_생산_001.pdf'), ('./고라니/', 'A_2022_04_10_생산로그_생산_002.pdf', 'A_2022_04_10_생산로그_생산_002.pdf'), ('./고라니/', 'A_2022_04_10_생산로그_생산_003.pdf', 'A_2022_04_10_생산로그_생산_003.pdf'), ('./고라니/', 'A_2022_04_10_생산로그_생산_004.pdf', 'A_2022_04_10_생산로그_생산_004.pdf'), ('./고라니/', 'A_2022_06_30_생산로그_생산_001.pdf', 'A_2022_06_30_생산로그_생산_001.pdf'), ('./고라니/', 'A_2022_06_30_생산로그_생산_002.pdf', 'A_2022_06_30_생산로그_생산_002.pdf'), ('./고라니/', 'A_2022_07_20_부서로그_인사_001.pdf', 'A_2022_07_20_부서로그_인사_001.pdf'), ('./고라니/', 'A_2022_07_20_부서로그_인사_002.pdf', 'A_2022_07_20_부서로그_인사_002.pdf'), ('./고라니/', 'A_2022_07_20_부서로그_인사_003.pdf', 'A_2022_07_20_부서로그_인사_003.pdf'), ('./고라니/', 'A_2022_07_20_부서로그_인사_004 (1).pdf', 'A_2022_07_20_부서로그_인사_004.pdf'), ('./고라니/', 'B_2022_02_20_상반기_클래스설계_001.xlsx', 'B_2022_02_20_상반기_클래스설계_001.xlsx'), ('./고라니/', 'B_2022_02_20_상반기_클래스설계_002.xlsx', 'B_2022_02_20_상반기_클래스설계_002.xlsx'), ('./고라니/', 'B_2022_06_20_하반기_클래스설계_001.xlsx', 'B_2022_06_20_하반기_클래스설계_001.xlsx'), ('./고라니/', 'B_2022_06_30_하반기_클래스설계_002.xlsx', 'B_2022_06_30_하반기_클래스설계_002.xlsx'), ('./고라니/', 'C_2022_03_30_데이터베이스_ERD_001.xlsx', 'C_2022_03_30_데이터베이스_ERD_001.xlsx'), ('./고라니/', 'C_2022_03_30_데이터베이스_ERD_002.xlsx', 'C_2022_03_30_데이터베이스_ERD_002.xlsx'), ('./고라니/', 'C_2022_03_30_데이터베이스_ERD_003.xlsx', 'C_2022_03_30_데이터베이스_ERD_003.xlsx'), ('./고라니/', 'C_2022_09_10_데이터베이스_ERD_001 (1).xlsx', 'C_2022_09_10_데이터베이스_ERD_001.xlsx'), ('./고라니/', 'C_2022_09_10_데이터베이스_ERD_002.xlsx', 'C_2022_09_10_데이터베이스_ERD_002.xlsx'), ('./고라니/', 'C_2022_09_10_데이터베이스_ERD_003.xlsx', 'C_2022_09_10_데이터베이스_ERD_003.xlsx'), ('./고라니/', 'filelist.xlsx', 'filelist.xlsx'), ('./고라니/', '내사진.jpg', '내사진.jpg'), ('./고라니/', '데이터저장_물류.zip', '데이터저장_물류.zip'), ('./고라니/', '새파일1_복사본.txt', '새파일1_복사본.txt'), ('./고라니/', '점심시간.txt', '저녁시간.txt'), ('./고라니/', '주피터노트북.txt', '주피터노트북.txt'))
(26)에서:
import shutil
def fileRename(datalist : list):
for data in datalist:
print(data(1) + '의 파일 명을 ' + data(2) + '로 변경합니다')
shutil.move(data(0)+data(1), data(0)+data(2))
(27)에서:
fileRename(rename_list)
A_2022_01_13_부서로그_인사_001.pdf의 파일 명을 A_2022_01_13_부서로그_인사_001.pdf로 변경합니다
A_2022_01_13_부서로그_인사_002.pdf의 파일 명을 A_2022_01_13_부서로그_인사_002.pdf로 변경합니다
A_2022_01_13_부서로그_인사_003.pdf의 파일 명을 A_2022_01_13_부서로그_인사_003.pdf로 변경합니다
A_2022_04_10_생산로그_생산_001.pdf의 파일 명을 A_2022_04_10_생산로그_생산_001.pdf로 변경합니다
A_2022_04_10_생산로그_생산_002.pdf의 파일 명을 A_2022_04_10_생산로그_생산_002.pdf로 변경합니다
A_2022_04_10_생산로그_생산_003.pdf의 파일 명을 A_2022_04_10_생산로그_생산_003.pdf로 변경합니다
A_2022_04_10_생산로그_생산_004.pdf의 파일 명을 A_2022_04_10_생산로그_생산_004.pdf로 변경합니다
A_2022_06_30_생산로그_생산_001.pdf의 파일 명을 A_2022_06_30_생산로그_생산_001.pdf로 변경합니다
A_2022_06_30_생산로그_생산_002.pdf의 파일 명을 A_2022_06_30_생산로그_생산_002.pdf로 변경합니다
A_2022_07_20_부서로그_인사_001.pdf의 파일 명을 A_2022_07_20_부서로그_인사_001.pdf로 변경합니다
A_2022_07_20_부서로그_인사_002.pdf의 파일 명을 A_2022_07_20_부서로그_인사_002.pdf로 변경합니다
A_2022_07_20_부서로그_인사_003.pdf의 파일 명을 A_2022_07_20_부서로그_인사_003.pdf로 변경합니다
A_2022_07_20_부서로그_인사_004 (1).pdf의 파일 명을 A_2022_07_20_부서로그_인사_004.pdf로 변경합니다
B_2022_02_20_상반기_클래스설계_001.xlsx의 파일 명을 B_2022_02_20_상반기_클래스설계_001.xlsx로 변경합니다
B_2022_02_20_상반기_클래스설계_002.xlsx의 파일 명을 B_2022_02_20_상반기_클래스설계_002.xlsx로 변경합니다
B_2022_06_20_하반기_클래스설계_001.xlsx의 파일 명을 B_2022_06_20_하반기_클래스설계_001.xlsx로 변경합니다
B_2022_06_30_하반기_클래스설계_002.xlsx의 파일 명을 B_2022_06_30_하반기_클래스설계_002.xlsx로 변경합니다
C_2022_03_30_데이터베이스_ERD_001.xlsx의 파일 명을 C_2022_03_30_데이터베이스_ERD_001.xlsx로 변경합니다
C_2022_03_30_데이터베이스_ERD_002.xlsx의 파일 명을 C_2022_03_30_데이터베이스_ERD_002.xlsx로 변경합니다
C_2022_03_30_데이터베이스_ERD_003.xlsx의 파일 명을 C_2022_03_30_데이터베이스_ERD_003.xlsx로 변경합니다
C_2022_09_10_데이터베이스_ERD_001 (1).xlsx의 파일 명을 C_2022_09_10_데이터베이스_ERD_001.xlsx로 변경합니다
C_2022_09_10_데이터베이스_ERD_002.xlsx의 파일 명을 C_2022_09_10_데이터베이스_ERD_002.xlsx로 변경합니다
C_2022_09_10_데이터베이스_ERD_003.xlsx의 파일 명을 C_2022_09_10_데이터베이스_ERD_003.xlsx로 변경합니다
filelist.xlsx의 파일 명을 filelist.xlsx로 변경합니다
내사진.jpg의 파일 명을 내사진.jpg로 변경합니다
데이터저장_물류.zip의 파일 명을 데이터저장_물류.zip로 변경합니다
새파일1_복사본.txt의 파일 명을 새파일1_복사본.txt로 변경합니다
점심시간.txt의 파일 명을 저녁시간.txt로 변경합니다
주피터노트북.txt의 파일 명을 주피터노트북.txt로 변경합니다
4. 폴더 생성
(28)에서:
import fnmatch
(33)에서:
def categoryList(target_path : str) -> list:
file_list = ()
for filename in os.listdir(target_path):
if fnmatch.fnmatch(filename, '*_(0-9)(0-9)(0-9).*'):
file_list.append(filename)
category = ()
for file in file_list:
temp_list = file.split('_')
category.append(temp_list(-2))
temp_set = set(category)
result = list(temp_set)
return result
(34)에서:
categoryList(target_path)
꺼짐(34):
('클래스설계', '생산', 'ERD', '인사')
(36)에서:
categorylist = categoryList(target_path) + ('기타')
print(categorylist)
('클래스설계', '생산', 'ERD', '인사', '기타')
(37)에서:
import pathlib
(42)에서:
new_path="./new_dir"
def makeDir(new_path:str, categorylist:list):
for category in categorylist:
new_dir = pathlib.Path(os.path.join(new_path, category))
new_dir.mkdir(parents=True, exist_ok=True)
(43)에서:
makeDir(new_path, categorylist)
5. 파일 분류 및 이동
(44)에서:
import shutil
(49)에서:
def moveFile(new_path, target_path, categorylist):
dirlist = os.listdir(new_path) # 이동시킬 경로에 생성된 분류 디렉토리 리스트
filelist = os.listdir(target_path) # 이동시킬 파일명 리스트
categorydic = {}
for file in filelist:
try:
temp_list = file.split('_')
assert temp_list(-2) in categorylist # 카테고리가 맞지 않으면 에러 발생
categorydic(file) = temp_list(-2) # {'파일명':'분류명'}
except:
categorydic(file) = '기타' # {'파일명':'기타'}
# print(categorydic)
for key, value in categorydic.items():
shutil.copy(target_path+'/'+key, new_path+'/'+value)
(50)에서:
moveFile(new_path, target_path, categorylist)