카페 24에서 이미지 업로드 하려는데 다음과 같은 에러가 떴다.

하나하나 바꿔주기에는 파일이 너무 많아서 하단 코드로 파일명을 바꿔주었다.
import os
import re
def rename_files_in_directory(directory_path):
# 이미지에 명시된 금지 캐릭터들 (마지막 점 포함)
# / * ? " < > | .
# 단, 확장자 앞의 점은 유지해야 하므로 정규식으로 처리합니다.
if not os.path.exists(directory_path):
print(f"오류: 경로를 찾을 수 없습니다 -> {directory_path}")
return
files = os.listdir(directory_path)
count = 0
for filename in files:
# 파일 전체 경로
old_path = os.path.join(directory_path, filename)
# 디렉토리가 아닌 파일인 경우에만 처리
if os.path.isfile(old_path):
# 파일명과 확장자 분리 (예: "my.file.txt" -> "my.file", ".txt")
name, ext = os.path.splitext(filename)
# 1. 파일 이름 부분에서 금지된 특수문자 제거/치환
# 정규식 [/*?\"<>|.] 은 해당 문자들 중 하나라도 있으면 매칭됨
# 여기서는 공백(Empty string)으로 대체하여 삭제 처리
clean_name = re.sub(r'[/*?\"<>|.]', '', name)
# 혹시 이름이 완전히 비어버릴 경우를 대비해 기본값 설정
if not clean_name:
clean_name = "unnamed_file"
# 새로운 파일명 생성 (깨끗해진 이름 + 기존 확장자)
new_filename = clean_name + ext
new_path = os.path.join(directory_path, new_filename)
# 실제 이름이 바뀔 필요가 있을 때만 실행
if filename != new_filename:
try:
# 파일명이 이미 존재하는 경우 방지
if os.path.exists(new_path):
new_path = os.path.join(directory_path, f"fixed_{new_filename}")
os.rename(old_path, new_path)
print(f"변경 완료: {filename} -> {os.path.basename(new_path)}")
count += 1
except Exception as e:
print(f"변경 실패 ({filename}): {e}")
print(f"\n총 {count}개의 파일 이름이 정리되었습니다.")
if __name__ == "__main__":
# 경로 설정
target_dir = r"C:\Users\이름\Downloads\폴더명"
rename_files_in_directory(target_dir)