티스토리 뷰

반응형

Stack Overflow에 자주 검색, 등록되는 문제들과 제가 개발 중 찾아 본 문제들 중에서 나중에도 찾아 볼 것 같은 문제들을 정리하고 있습니다.

Stack Overflow에서 가장 먼저 확인하게 되는 가장 높은 점수를 받은 Solution과 현 시점에 도움이 될 수 있는 가장 최근에 업데이트(최소 점수 확보)된 Solution을 각각 정리하였습니다.

 

아래 word cloud를 통해 이번 포스팅의 주요 키워드를 미리 확인하세요.

python: delete non-empty dir

파이썬: 비어 있지 않은 디렉토리 삭제

 문제 내용 

How do I delete a possibly non-empty dir in Python.

파이썬에서 비어 있지 않은 디렉토리를 삭제하는 방법입니다.

 

The directory may have nested subdirectories many levels deep.

해당 디렉토리는 여러 단계 아래에 중첩된 하위 디렉토리를 가지고 있을 수 있습니다.

 

 

 

 높은 점수를 받은 Solution 

Use shutil.rmtree:

shutil.rmtree를 사용하세요.
import shutil

shutil.rmtree(path)

 

See the documentation for details of how to handle and/or ignore errors.

에러를 처리하거나 무시하는 방법에 대한 자세한 내용은 문서를 참조하세요.

 

 

 

 가장 최근 달린 Solution 

The standard library includes shutil.rmtree for this. By default,

Python 표준 라이브러리에는 이를 위한 shutil.rmtree가 포함되어 있습니다. 기본적으로,
shutil.rmtree(path)  # errors if dir not empty

 

will give OSError: [Errno 66] Directory not empty: <your/path>.

다음과 같이 디렉토리를 삭제하려고 할 때, OSError: [Errno 66] Directory not empty: <your/path> 에러가 발생할 수 있습니다.

 

You can delete the directory and its contents anyway by ignoring the error:

에러를 무시하고 디렉토리와 그 내용을 모두 삭제할 수 있습니다.
shutil.rmtree(role_fs_path, ignore_errors=True)

 

You can perform more sophisticated error handling by also passing onerrror=<some function(function, path, excinfo)>.

onerrror=<some function(function, path, excinfo)> 를 전달하여 더 세련된 오류 처리를 수행할 수 있습니다.

 

 

 

출처 : https://stackoverflow.com/questions/1557351/python-delete-non-empty-dir

반응형
댓글
공지사항
최근에 올라온 글