40 lines
1.8 KiB
Python
40 lines
1.8 KiB
Python
import os
|
|
import shutil
|
|
import re
|
|
|
|
# 검색할 루트 디렉터리와 파일 복사 대상 루트 디렉터리 설정
|
|
root_dir = r"./" # 탐색할 루트 디렉터리
|
|
output_root_dir = r"./" # 복사된 파일을 저장할 디렉터리
|
|
|
|
# 필요한 폴더 이름
|
|
target_folders = ["1교시", "2교시", "3교시"]
|
|
|
|
# 정규식 패턴
|
|
pattern = r'^워드\(한글\).*\.hwp$'
|
|
|
|
# 출력 디렉터리에 '1교시', '2교시', '3교시' 폴더 생성
|
|
for folder_name in target_folders:
|
|
os.makedirs(os.path.join(output_root_dir, folder_name), exist_ok=True)
|
|
|
|
# 루트 디렉터리 탐색
|
|
for dirpath, dirnames, filenames in os.walk(root_dir):
|
|
for target_folder in target_folders:
|
|
if target_folder in dirpath: # 현재 경로에 '1교시', '2교시', '3교시'가 포함되어 있는지 확인
|
|
for file in filenames:
|
|
if re.match(pattern, file): # 파일 이름이 정규식 패턴과 일치하는지 확인
|
|
source_file = os.path.join(dirpath, file)
|
|
destination_folder = os.path.join(output_root_dir, target_folder)
|
|
destination_file = os.path.join(destination_folder, file)
|
|
|
|
# 동일한 이름의 파일이 있으면 이름에 번호 추가
|
|
counter = 1
|
|
while os.path.exists(destination_file):
|
|
base, ext = os.path.splitext(file)
|
|
destination_file = os.path.join(destination_folder, f"{base}_{counter}{ext}")
|
|
counter += 1
|
|
|
|
# 파일 복사
|
|
shutil.copy2(source_file, destination_file)
|
|
print(f"복사 완료: {source_file} → {destination_file}")
|
|
|
|
print("모든 .hwp 파일 추출 완료!") |