Pathon删除指定文件夹下的文件夹和文件

import os
import shutil
from pathlib import Path
def delFileAndDir(delDir):
    if Path(delDir).exists():
        delList = os.listdir(delDir)
        for f in delList:
            filePath = os.path.join(delDir,f)
            if os.path.isfile(filePath):
                os.remove(filePath)
            elif os.path.isdir(filePath):
                shutil.rmtree(filePath,True)
原文地址:https://www.cnblogs.com/yanweichen/p/15255247.html