这里为大家提供一个删除windows目录下文件的python代码,供大家学习参考。
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)
小插曲:
刚开始没有引入import shutil 。
而这句 shutil.rmtree(filePath,True) 提示未定义。