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)

 

posted on 2021-09-11 14:50  达摩克利斯  阅读(91)  评论(0编辑  收藏  举报

导航