๐ŸŸจ Dev.-Python

ํŒŒ์ด์ฌ ์„ค์น˜ํ•˜๊ธฐ ๋ฐ VS code ์‹คํ–‰ + png ์ด๋ฏธ์ง€ ์ด๋ฆ„ ์•ž๊ธ€์ž ๋„˜๋ฒ„๋ง์„ ์ˆ˜์ •๋œ ๋‚ ์งœ ์ˆœ์œผ๋กœ ๋ฒˆํ˜ธ ์ถ”๊ฐ€ํ•˜๊ธฐ ํ”„๋กœ๊ทธ๋žจ (.png ์•ž ์ˆซ์ž ์ „๋ถ€ ์‚ญ์ œํ•˜๊ณ  ์ˆซ์ž๋งค๊น€)

ramen2 2025. 2. 2. 00:30
๋ฐ˜์‘ํ˜•

 

1. ํŒŒ์ด์ฌ ์„ค์น˜ ํ™•์ธ

1. ์‚ฌ์ดํŠธ์—์„œ ํŒŒ์ด์ฌ ๋‹ค์šด๋กœ๋“œ

 

 

Download Python

The official home of the Python Programming Language

www.python.org

 

 

 

 

 

2. ์„ค์น˜ํ•  ๋•Œ "Add Python to PATH" ์˜ต์…˜ ์ฒดํฌํ•˜๊ธฐ

 

 

 

3. VS Code ํ„ฐ๋ฏธ๋„(๋˜๋Š” cmd ์ฐฝ)์—์„œ ์•„๋ž˜ ๋ช…๋ น์–ด ์‹คํ–‰ (์„ค์น˜๋˜์–ด ์žˆ๋‹ค๋ฉด+์•„๋ž˜ ํ™˜๊ฒฝ๋ณ€์ˆ˜๊นŒ์ง€ ์ž˜ ์ ์šฉ๋๋‹ค๋ฉด ๋ฒ„์ „์ด ๋‚˜์˜ด.)

python --version

์ด๋Ÿฐ์‹์œผ๋กœ ์œ„์—์„ค์น˜ํ•œ ๋ฒ„์ „๋‚˜์˜ด (4. ํ™˜๊ฒฝ๋ณ€์ˆ˜๊นŒ์ง€ ํ•ด์•ผํ•จ)

 

 

 

4. ํ™˜๊ฒฝ๋ณ€์ˆ˜ ํŽธ์ง‘ ์ง„์ž…

ํด๋ฆญ
ํŽธ์ง‘
ํŽธ์ง‘

 

 

5. ์œˆ๋„์šฐ cmd -> ์•„๋ž˜ ๋ช…๋ น์–ด ์‹คํ–‰ -> ํŒŒ์ด์„ ์ด ์„ค์น˜๋œ ๊ฒฝ๋กœ ๋ณต์‚ฌ ํ›„ ์œ„ ํ™˜๊ฒฝ๋ณ€์ˆ˜์— ์ถ”๊ฐ€ํ•ด์คŒ

where python

 

 

 

 

 

 

2. VS Code์—์„œ ํŒŒ์ด์ฌ ์‹คํ–‰ ๋ฐฉ๋ฒ•

 

 

  1. VS Code์—์„œ ์ƒˆ ํด๋” ์ƒ์„ฑ (์˜ˆ: folder_name)
  2. ๊ทธ ์•ˆ์— rename_files.py ํŒŒ์ผ ๋งŒ๋“ค๊ธฐ
  3. ์•„๋ž˜ ์ฝ”๋“œ ์ž‘์„ฑ
  4. VS Code์—์„œ ํ„ฐ๋ฏธ๋„ ์—ด๊ธฐ (Ctrl + ~)
  5. python rename_files.py ์ž…๋ ฅ ํ›„ ์‹คํ–‰

 

 


 

โš ๏ธ pyํŒŒ์ผ๊ณผ ํ•จ๊ป˜ ์žˆ๋Š” ํด๋”(name_change_pngs) ์† ์ด๋ฏธ์ง€ ์ด๋ฆ„์„ ๋ณ€๊ฒฝํ•ด์คŒ 

ํด๋”๊ตฌ์„ฑ

- ์žฌ์‚ฌ์šฉ์„ฑ ์ข‹์€ ์ฝ”๋“œ

- ๋‹ค๋ฅธ PC์—์„œ๋„ ํด๋” ๊ตฌ์กฐ๋งŒ ์œ ์ง€ํ•˜๋ฉด ์‹คํ–‰๋จ

- Python ํŒŒ์ผ์ด ์–ด๋”” ์žˆ๋“  ์‹คํ–‰ ๊ฐ€๋Šฅํ•จ

 

#--------------- ๋‚ ์งœ๊ฐ€๊นŒ์šด ์ˆœ์œผ๋กœ 1,2,3 ์ˆซ์ž์ด๋ฆ„ ์ถ”๊ฐ€ํ•˜๋Š” ์ฝ”๋“œ
import os
from datetime import datetime

# ํ˜„์žฌ ์‹คํ–‰ ์ค‘์ธ ํŒŒ์ผ(`.py`)์ด ์žˆ๋Š” ํด๋”์˜ ๊ฒฝ๋กœ
base_dir = os.path.dirname(os.path.abspath(__file__))

# ๋Œ€์ƒ ํด๋” (์ด ํด๋” ์•ˆ์˜ png ํŒŒ์ผ ์ด๋ฆ„ ๋ณ€๊ฒฝ)
target_folder = os.path.join(base_dir, "name_change_pngs")

# ํด๋” ๋‚ด ๋ชจ๋“  PNG ํŒŒ์ผ ์ฐพ๊ธฐ
file_info = []

# ํŒŒ์ผ๋“ค์˜ ์ˆ˜์ • ์‹œ๊ฐ„์„ ๊ฐ€์ ธ์™€์„œ ๋ฆฌ์ŠคํŠธ์— ์ €์žฅ
for filename in os.listdir(target_folder):
    if filename.endswith(".png"):
        old_path = os.path.join(target_folder, filename)

        # ์ˆซ์ž ์ ‘๋‘์‚ฌ ์ œ๊ฑฐ (์ˆซ์ž์™€ ์–ธ๋”์Šค์ฝ”์–ด(_)๋Š” ๋ชจ๋‘ ์ œ๊ฑฐ)
        new_filename = ''.join([char for char in filename if not char.isdigit() and char != '_'])

        # ์ด๋ฆ„ ๋ณ€๊ฒฝ ํ›„ ํŒŒ์ผ์ด ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธ
        if old_path != os.path.join(target_folder, new_filename):
            new_path = os.path.join(target_folder, new_filename)
            os.rename(old_path, new_path)
            old_path = new_path  # ์ด๋ฆ„ ๋ณ€๊ฒฝ ํ›„ ์ƒˆ๋กœ์šด ๊ฒฝ๋กœ๋กœ ์—…๋ฐ์ดํŠธ

        # ์ˆ˜์ • ์‹œ๊ฐ„ ๊ฐ€์ ธ์˜ค๊ธฐ
        modification_time = os.path.getmtime(old_path)
        file_info.append((new_filename, modification_time, old_path))

# ์ˆ˜์ • ์‹œ๊ฐ„์„ ๊ธฐ์ค€์œผ๋กœ ํŒŒ์ผ๋“ค์„ ์ •๋ ฌ (๊ฐ€์žฅ ์ตœ๊ทผ ์ˆ˜์ •๋œ ํŒŒ์ผ๋ถ€ํ„ฐ)
file_info.sort(key=lambda x: x[1], reverse=True)

# ํŒŒ์ผ ๋ฒˆํ˜ธ ๋งค๊ธฐ๊ธฐ
for index, (filename, _, old_path) in enumerate(file_info, start=1):
    # ์ƒˆ ๋ฒˆํ˜ธ ๋ถ™์—ฌ์„œ ํŒŒ์ผ ์ด๋ฆ„ ์„ค์ •
    new_filename = f"{index}_{filename}"
    new_path = os.path.join(target_folder, new_filename)

    # ์ด๋ฆ„ ๋ณ€๊ฒฝ ํ›„ ํŒŒ์ผ์ด ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธ
    if os.path.exists(old_path):
        os.rename(old_path, new_path)
        print(f"๋ณ€๊ฒฝ ์™„๋ฃŒ: {filename} → {new_filename}")
    else:
        print(f"ํŒŒ์ผ์ด ์กด์žฌํ•˜์ง€ ์•Š์Œ: {old_path}")

 

 

 

 

3. ๊ฒฐ๊ณผ

 

์ง€์ •ํ•œ ํด๋”์— ์žˆ๋Š” .pngํŒŒ์ผ๋“ค์˜ ์ด๋ฆ„ ์•ž์— ์ˆ˜์ •๋œ ์ˆœ์œผ๋กœ ๋„˜๋ฒ„๋ง์ด ๋ถ™์Œ(์‚ฌ์ง„ ์‚ญ์ œํ•˜๊ณ  ๋‹ค์‹œ ์‹คํ–‰์‹œํ‚ค๋ฉด ์žฌํ• ๋‹น ๋จ)

  • ์˜ˆ: photo.png→ 1_photo.png

 

 

 

 

 

 

 

+++ ํŠน์ • ๊ธ€์ž๋งŒ ์ถ”๊ฐ€/์ œ๊ฑฐ ํ•˜๊ณ ์‹ถ์„์‹œ ์ฝ”๋“œ


#--------------------------------------------- [์ถ”๊ฐ€๋ ๊ธ€์ž] ์ถ”๊ฐ€ํ•˜๋Š” ์ฝ”๋“œ
# import os

# # ํ˜„์žฌ ์‹คํ–‰ ์ค‘์ธ ํŒŒ์ผ(`.py`)์ด ์žˆ๋Š” ํด๋”์˜ ๊ฒฝ๋กœ
# base_dir = os.path.dirname(os.path.abspath(__file__))

# # ๋Œ€์ƒ ํด๋” (์ด ํด๋” ์•ˆ์˜ png ํŒŒ์ผ ์ด๋ฆ„ ๋ณ€๊ฒฝ)
# target_folder = os.path.join(base_dir, "name_change_pngs")

# # ํด๋” ๋‚ด ๋ชจ๋“  PNG ํŒŒ์ผ ์ฐพ๊ธฐ``
# for filename in os.listdir(target_folder):
#     if filename.endswith(".png"):
#         old_path = os.path.join(target_folder, filename)
#         new_path = os.path.join(target_folder, f"[์ถ”๊ฐ€๋ ๊ธ€์ž]{filename}")

#         os.rename(old_path, new_path)
#         print(f"๋ณ€๊ฒฝ ์™„๋ฃŒ: {filename} → [์ถ”๊ฐ€๋ ๊ธ€์ž]{filename}")



#--------------------------------------------- [์ถ”๊ฐ€๋ ๊ธ€์ž] ์—†์• ๋Š” ์ฝ”๋“œ
# import os

# # ํ˜„์žฌ ์‹คํ–‰ ์ค‘์ธ ํŒŒ์ผ(`.py`)์ด ์žˆ๋Š” ํด๋”์˜ ๊ฒฝ๋กœ
# base_dir = os.path.dirname(os.path.abspath(__file__))

# # ๋Œ€์ƒ ํด๋” (์ด ํด๋” ์•ˆ์˜ png ํŒŒ์ผ ์ด๋ฆ„ ๋ณ€๊ฒฝ)
# target_folder = os.path.join(base_dir, "name_change_pngs")

# # ํด๋” ๋‚ด ๋ชจ๋“  PNG ํŒŒ์ผ ์ฐพ๊ธฐ
# if os.path.exists(target_folder):  # ํด๋”๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธ
#     for filename in os.listdir(target_folder):
#         if filename.startswith("[์ถ”๊ฐ€๋ ๊ธ€์ž]") and filename.endswith(".png"):
#             old_path = os.path.join(target_folder, filename)
#             new_path = os.path.join(target_folder, filename.replace("[์ถ”๊ฐ€๋ ๊ธ€์ž]", "", 1))  # "[์ถ”๊ฐ€๋ ๊ธ€์ž]" ์ œ๊ฑฐ

#             os.rename(old_path, new_path)
#             print(f"๋ณ€๊ฒฝ ์™„๋ฃŒ: {filename} → {new_path}")
# else:
#     print(f"ํด๋”๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Œ: {target_folder}")

 

 

 

 

๋ฐ˜์‘ํ˜•