[파이썬 Projects]/<파이썬 업무 자동화>
[파이썬] 파일명 변경하기 ver.3
기록자_Recordian
2024. 9. 7. 12:14
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
반응형