<aside> 💡 shutil 모듈은 파일 및 디렉토리 작업을 위한 다양한 기능을 제공합니다.

</aside>

ready.py

# shutil 실습을 위한 초기화

import os

animal_filename = "Tiger.txt"
animal_description = "호랑이는 크고 강력한 고양잇과 동물로, 아름다운 줄무늬가 특징입니다."

with open(animal_filename, "w", encoding="utf-8") as file:
    file.write(animal_description)

os.makedirs("fruit", exist_ok=True)
os.makedirs("animal", exist_ok=True)

fruits = {
    "Apple.txt": "사과는 달콤하고 아삭아삭한 과일로, 여러 가지 종류가 있습니다.",
    "Banana.txt": "바나나는 영양가가 높고 에너지를 빠르게 제공하는 열대 과일입니다.",
    "Cherry.txt": "체리는 작고 빨간색이며, 달콤하고 약간의 신맛이 나는 과일입니다.",
    "Grape.txt": "포도는 달콤하며 여러 개가 한 송이로 모여 있는 과일입니다.",
    "Orange.txt": "오렌지는 비타민 C가 풍부하고 새콤달콤한 맛이 나는 과일입니다."
}

for filename, description in fruits.items():
    with open(os.path.join("fruit", filename), "w", encoding="utf-8") as file:
        file.write(description)

🛑 아래부터는 각 코드를 실행 후 비활성화하며 진행

import os
import shutil

# 파일 복사
shutil.copy("Tiger.txt", "./animal")
# 파일 다른 이름으로 복사
shutil.copy("Tiger.txt", "./animal/Tigers_copy.txt")
os.makedirs("beast", exist_ok=True)

# 파일 이동
shutil.move("Tiger.txt", "./beast")
# 이름 변경에도 사용 가능
shutil.move("./animal/Tigers_copy.txt", "./animal/Tigers_2.txt")
os.makedirs("./fruit/tropic", exist_ok=True)
shutil.move("./fruit/Banana.txt", "./fruit/tropic")
os.makedirs("plant", exist_ok=True)
# 폴더 내용을 [재귀적]으로 복사
shutil.copytree("./fruit", "./plant/fruit")
# 폴더를 재귀적으로 삭제
shutil.rmtree("./plant")