728x90
반응형
이번엔 접미사가 아닌
'tire_image_42.jpg_non_tire'
'tire_image_45.jpg_tire'
처럼 중간에 '.jpg' 가 붙어 있는 파일들의 파일명을 변경하려고 한다.
import os
# 바꿔야할 폴더 경로
directory = r'C:\Users\niceq\Desktop\Startup-related\Tire Scanner\Tire images\Classified Images'
# List all files in the directory
for filename in os.listdir(directory):
# Construct old file path
old_file_path = os.path.join(directory, filename)
# Check if file name contains '.jpg_' and needs renaming
if '.jpg_' in filename:
# Create new file name by removing '.jpg_'
new_filename = filename.replace('.jpg_', '_')
# Construct new file path
new_file_path = os.path.join(directory, new_filename)
# Rename the file
os.rename(old_file_path, new_file_path)
print(f"Renamed: {old_file_path} -> {new_file_path}")
print("Renaming completed.")
역시 아주 빠르게 해결됐다.
728x90
반응형
'[파이썬 Projects] > <파이썬 업무 자동화>' 카테고리의 다른 글
[파이썬] 대량의 폴더 생성하기 (1) | 2024.09.07 |
---|---|
[파이썬] 폴더 내의 파일 분류시키기 (0) | 2024.09.07 |
[파이썬] 파일명 변경하기 ver.2 (2) | 2024.09.06 |
[이미지 웹스크래핑] 픽사베이 API Key 발급을 통한 이미지 수집 (1) | 2024.09.05 |
[파이썬] 폴더 내의 여러 파일의 파일명 한 번에 바꾸기 (1) | 2024.09.05 |