Python은 파일 복사, 파일 이동, 재귀적으로 폴더 삭제, 이름 바꾸기, 압축 및 압축 해제를 위해 shutil을 사용합니다.

shutil은 파일 및 디렉터리 복사, 삭제 및 이동과 같은 작업을 처리하기 위한 Python 표준 라이브러리의 모듈입니다. 일반적으로 사용되는 몇 가지 기능은 다음과 같습니다 shutil.

복사 복사

copy(src, dest): src소스 파일을 대상 파일에 복사합니다 dest.

import shutil

shutil.copy("d:/python3_test/one.txt", "d:/python3_test/two.txt")

copytree(src, dest): src소스 디렉토리와 하위 디렉토리 및 파일을 대상 디렉토리에 복사합니다 dest.

파일 이동

move(src, dest): 소스 파일 또는 디렉터리를 대상 파일 또는 디렉터리로 src이동하거나 이름을 바꿉니다 dest.

import shutil

shutil.move("d:/python3_test/one.txt", "d:/python3_test/three.txt")

재귀적으로 폴더 삭제

rmtree(path): 지정된 디렉토리 path와 하위 디렉토리 및 파일을 반복적으로 삭제합니다.

중명명

rename(old, new): 파일 또는 디렉토리 의 old이름을 new.

압축

import shutil

shutil.make_archive("archive_name", 'zip', root_dir="D:\python3_test", base_dir="shutil_test")

링크

link(src, dest): src대상 파일 또는 디렉터리 dest에 대한 원본 파일 또는 디렉터리의 하드 링크를 만듭니다. : 대상 파일 또는 디렉토리에 대한 소스 파일 또는 디렉토리의 심볼릭 링크를
symlink(src, dest)만듭니다 .srcdest

참고

https://docs.python.org/zh-cn/3/library/shutil.html

추천

출처blog.csdn.net/lilongsy/article/details/132068964