[파이썬] shutil 동적으로 변화하는 디렉토리 구조 관리하기

파이썬의 shutil 모듈은 파일 및 디렉토리 작업을 위한 유용한 함수와 클래스를 제공합니다. 이 모듈을 사용하여 동적으로 변화하는 디렉토리 구조를 효과적으로 관리할 수 있습니다.

디렉토리 복사하기

shutil 모듈의 copytree 함수를 사용하여 디렉토리를 복사할 수 있습니다. 다음은 src 디렉토리를 dst 디렉토리로 복사하는 예제입니다.

import shutil

src = 'path/to/src'
dst = 'path/to/dst'

shutil.copytree(src, dst)

디렉토리 이동하기

shutil 모듈의 move 함수를 사용하여 디렉토리를 이동할 수 있습니다. 다음은 src 디렉토리를 dst 디렉토리로 이동하는 예제입니다.

import shutil

src = 'path/to/src'
dst = 'path/to/dst'

shutil.move(src, dst)

디렉토리 삭제하기

shutil 모듈의 rmtree 함수를 사용하여 디렉토리를 삭제할 수 있습니다. 다음은 dir 디렉토리를 삭제하는 예제입니다.

import shutil

dir = 'path/to/dir'

shutil.rmtree(dir)

디렉토리 이름 변경하기

shutil 모듈의 move 함수를 사용하여 디렉토리의 이름을 변경할 수 있습니다. 다음은 old_name 디렉토리의 이름을 new_name으로 변경하는 예제입니다.

import shutil

old_name = 'path/to/old_name'
new_name = 'path/to/new_name'

shutil.move(old_name, new_name)

파일 복사하기

shutil 모듈의 copy 함수를 사용하여 파일을 복사할 수 있습니다. 다음은 src_filedst_file로 복사하는 예제입니다.

import shutil

src_file = 'path/to/src_file'
dst_file = 'path/to/dst_file'

shutil.copy(src_file, dst_file)

파일 이동하기

shutil 모듈의 move 함수를 사용하여 파일을 이동할 수 있습니다. 다음은 src_filedst_file로 이동하는 예제입니다.

import shutil

src_file = 'path/to/src_file'
dst_file = 'path/to/dst_file'

shutil.move(src_file, dst_file)

파일 삭제하기

shutil 모듈의 remove 함수를 사용하여 파일을 삭제할 수 있습니다. 다음은 file을 삭제하는 예제입니다.

import shutil

file = 'path/to/file'

shutil.remove(file)

shutil 모듈을 사용하여 디렉토리와 파일을 효과적으로 관리할 수 있습니다. 다양한 작업을 수행하는 함수와 클래스를 활용하여 동적으로 변화하는 디렉토리 구조를 쉽게 관리할 수 있습니다.