폴더 안에 모든 내용물을 다 지우는 기능을 찾고 있습니다

조회수 8670회

발생하는 문제 및 실행환경

폴더 안에 모든 내용물을 다 지우는 기능을 찾고 있습니다

찾아봤는데 os.rmdir()은 빈 디렉토리만 지울수 있어서(재귀호출해서 직접 만들 순 있지만)

혹시 안에 내용물까지 같이 지워주는 함수가 이미 있는지 알고 싶어서 질문합니다

1 답변

  • 좋아요

    0

    싫어요
    채택 취소하기

    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는 무시됩니다.

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)