๐ŸŸจ Dev.-Python

ํŒŒ์ด์ฌ ์‹คํ–‰ํŒŒ์ผ .bat / .exe ๋งŒ๋“ค๊ธฐ (batํŒŒ์ผ ํ•œ๊ธ€๊นจ์ง ํ•ด๊ฒฐ๋ฐฉ๋ฒ• / pngํŒŒ์ผ ๋„˜๋ฒ„๋ง exe ํŒŒ์ด์ฌ ์˜ˆ์ œ๊ณต์œ )

ramen2 2025. 2. 2. 01:16
๋ฐ˜์‘ํ˜•

โœ…์ด์ „์˜ ๋„˜๋ฒ„๋ง ํ”„๋กœ๊ทธ๋žจ์— ์ด์–ด์„œ ์ž‘์„ฑ

https://gsbd.tistory.com/83

 

 

| bat ํŒŒ์ผ ๋งŒ๋“ค๊ธฐ (๋งค์šฐ ๊ฐ„๋‹จํ•œ ๋ฐฉ๋ฒ•)

1. ๋ฉ”๋ชจ์žฅ์„ ์—ด๊ณ  ์•„๋ž˜ ๋‚ด์šฉ์„ ์ž…๋ ฅ:

@echo off
python "C:\Users\๋‚ด_ํด๋”\python_test\rename_files.py"
pause

 

2. Rename.bat์œผ๋กœ ์ €์žฅ (ํŒŒ์ผ ํ˜•์‹์„ .txt๊ฐ€ ์•„๋‹ˆ๋ผ .bat์œผ๋กœ ๋ณ€๊ฒฝ)

์ฃผ์˜ : batํŒŒ์ผ ์‹คํ–‰๊ฒฝ๋กœ์— ํ•œ๊ธ€์ด ์žˆ์œผ๋ฉด ์ธ์ฝ”๋”ฉ ์˜ค๋ฅ˜๋กœ ๊ฒฝ๋กœ๊ฐ€ ์ด์ƒํ•ด์ง€๊ธฐ ๋•Œ๋ฌธ์—

txt์—์„œ bat์œผ๋กœ ๋ฐ”๊ฟ€๋•Œ ์ธ์ฝ”๋”ฉ์„ ๊ผญ ANSI๋กœ ํ•จ


3. ๋ฐ”ํƒ•ํ™”๋ฉด์— ๋ฐ”๋กœ๊ฐ€๊ธฐ ์•„์ด์ฝ˜ ์ƒ์„ฑ

  • run_python.bat ํŒŒ์ผ์„ ๋งˆ์šฐ์Šค ์šฐํด๋ฆญ → ๋ฐ”๋กœ ๊ฐ€๊ธฐ ๋งŒ๋“ค๊ธฐ
  • ์›ํ•˜๋Š” ์•„์ด์ฝ˜์œผ๋กœ ๋ณ€๊ฒฝ ๊ฐ€๋Šฅ (์†์„ฑ → ์•„์ด์ฝ˜ ๋ณ€๊ฒฝ)

์ด๊ฑธ ๋ˆ„๋ฅด๋ฉด ๋™์ž‘

 

 

 

 

 

 

 

 

 

 

| exe ํŒŒ์ผ ๋งŒ๋“ค๊ธฐ (build)

0 . ์ฝ”๋“œ์ค€๋น„ (bulid๋กœ ๋งŒ๋“ค์–ด์ง„ exe์™€ ์ด๋ฆ„๋ฐ”๊ฟ€ ํด๋”๊ฐ€ ๊ฐ™์€ ๊ณณ์— ์žˆ์–ด์•ผํ•จ < exe ์‹คํ–‰์‹œ ํด๋” ์ž๋™ ์ƒ์„ฑ๋จ)

import os
import sys
from datetime import datetime

# ํ˜„์žฌ ์‹คํ–‰ ์ค‘์ธ ํŒŒ์ผ(.exe ๋˜๋Š” .py)์ด ์žˆ๋Š” ํด๋” ๊ฒฝ๋กœ
base_dir = os.path.dirname(sys.executable) if getattr(sys, 'frozen', False) else os.path.dirname(os.path.abspath(__file__))

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

# ํด๋”๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฉด ์ƒ์„ฑ
if not os.path.exists(target_folder):
    os.makedirs(target_folder)

# ํด๋” ๋‚ด ๋ชจ๋“  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}")

 

 

 

 

1. pyinstaller ์„ค์น˜ (VS Code ํ„ฐ๋ฏธ๋„ ๋˜๋Š” cmd์—์„œ ์‹คํ–‰)

pip install pyinstaller

 

2. Python ํŒŒ์ผ์„ ์‹คํ–‰ ๊ฐ€๋Šฅํ•œ EXE๋กœ ๋ณ€ํ™˜

pyinstaller --onefile rename_RUN.py

 

 

3. dist/rename_files.exe ํŒŒ์ผ์ด ์ƒ์„ฑ๋จ → ์‹คํ–‰์‹œ ์ด๋ฆ„๋ฐ”๊ฟ€ ํด๋” ์ƒ์„ฑ๋˜๋ฏ€๋กœ ๊ฑฐ๊ธฐ์— ํŒŒ์ผ๋„ฃ์–ด์„œ ์‚ฌ์šฉ

 

 

 

์‹คํ–‰์ „ / ํ›„
exe ์‹คํ–‰์‹œ ๋‚˜์˜ค๋Š” log

 

 

 

ํ˜ผ์ž ๊ฐ€๋ณ๊ฒŒ ์“ธ๊ฑฐ๋ผ๋ฉด exe bulid ๋ณด๋‹ค๋Š” .bat ์ถ”์ฒœ!
(์ˆ˜์ •ํ• ๋•Œ๋งˆ๋‹ค exe๋‹ค์‹œ ๋งŒ๋“ค์–ด์•ผํ•จ)

 

 

 

 

๐Ÿ“Œpng์ด๋ฏธ์ง€ ๋„˜๋ฒ„๋ง.EXE๐Ÿ“Œ

์ด๋ฆ„๋„˜๋ฒ„๋งํŒŒ์ด์„ ์˜ˆ์ œ.zip
15.03MB

 

 

๋ฐ˜์‘ํ˜•