python删除整个目录(目录非空)

2015-06-18 0 753
python删除整个目录(目录非空)

python删除整个目录(目录非空),os.rmdir()只能删除空目录,下面的函数可以删除整个非空目录

import os
import shutil
 
def CleanDir( Dir ):
    if os.path.isdir( Dir ):
        paths = os.listdir( Dir )
        for path in paths:
            filePath = os.path.join( Dir, path )
            if os.path.isfile( filePath ):
                try:
                    os.remove( filePath )
                except os.error:
                    autoRun.exception( "remove %s error." %filePath )#引入logging
            elif os.path.isdir( filePath ):
                if filePath[-4:].lower() == ".svn".lower():
                    continue
                shutil.rmtree(filePath,True)
    return True
 
Dir = "D:\\Temp"
CleanDir(Dir)

遇见资源网 python python删除整个目录(目录非空) http://www.ox520.com/15533.html

常见问题

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务