0
모든 폴더와 그 내용을 삭제하지만 개별 파일은 삭제하지 않는 파이썬 코드를 작성하려고합니다.
예를 들어, 삭제를 수행하는 스크립트 파일과 함께 디렉토리 (폴더 B)에 포함 된 일부 파일 및 폴더가 있습니다. folderA, folderB, folderC 등을 삭제하려면 어떻게하나요? 감사합니다파이썬 모든 폴더를 지우지 만 파일이 아님
/Folder B
file.docx
fileB.docx
fileC.docx
pythonDeleteScript.py
folderA/
folderB/
folderC/
folderD/
'shutil'과'os' 모듈을 보셨습니까? 후자를 사용하여 디렉토리를 검색하고 디렉토리 항목 만 유지하도록 필터 할 수 있으며 전자는 폴더 트리를 삭제할 수 있습니다. 그래서 실제로 코드 작성 서비스로 의도 된 것은 아니며,이 'shutil'태그를 지정 했으므로 모듈을 분명히 알 수 있습니다. 'os' /'os.path'를 살펴보고 스스로 해결해보십시오. – ShadowRanger
[os.path.isdir] (https://docs.python.org/3/library/os.path.html#os.path.isdir) + [os.listdir] (https : //docs.python. org/3/library/os.html # os.listdir) + [os.rmdir] (https://docs.python.org/3/library/os.html#os.rmdir) – Copperfield
@Copperfield : 당신은 폴더가 비어 있지 않은 경우 간단히 이렇게하려면 ['shutil.rmtree'] (https://docs.python.org/3/library/shutil.html#shutil.rmtree)를 원하십시오. 최신 Python을 사용하고 있다면,''os.scandir' (https://docs.python.org/3/library/os.html#os.scandir) (' os.listdir'와'os.path.isdir', 그러나보다 효율적으로) +'shutil.rmtree'. – ShadowRanger