폴더 안에 모든 내용물을 다 지우는 기능을 찾고 있습니다
조회수 8672회
발생하는 문제 및 실행환경
폴더 안에 모든 내용물을 다 지우는 기능을 찾고 있습니다
찾아봤는데 os.rmdir()은 빈 디렉토리만 지울수 있어서(재귀호출해서 직접 만들 순 있지만)
혹시 안에 내용물까지 같이 지워주는 함수가 이미 있는지 알고 싶어서 질문합니다
1 답변
-
shutil 모듈에 shutil.rmtree가 있습니다.
사용법은 대략 밑과 같고,
import shutil shutil.rmtree('/path/to/folder')
문서를 보면 shutil.rmtree(path, ignore_errors=False, onerror=None)
Delete an entire directory tree; path must point to a directory (but not a symbolic link to a directory). If ignore_errors is true, errors resulting from failed removals will be ignored; if false or omitted, such errors are handled by calling a handler specified by onerror or, if that is omitted, they raise an exception.
전체 디렉토리 tree를 지웁니다. path는 디렉토리여야만 합니다(디렉토리를 가리키는 심볼릭 링크의 경우는 포함하지 않습니다).
ignore_errors
가 true인 경우, 제거에 실패해도 error는 무시됩니다.
댓글 입력