玩蛇网提供最新Python编程技术信息以及Python资源下载!

python删除指定后缀名文件和目录

python 培训

这里我是用删除SVN目录和.o文件来举例说明,python删除指定后缀名文件和目录的方法。

title = 'this is a test for del svn directorys'

import os
import sys
import stat
import time

DEL_DIR=['.svn', '.CVS']
DEL_FILE=['.bak', '.back','.o']
log='delsvn.log'

def creatlog(object):
    if os.path.isdir(object):
        name = object + "\\" + log
    else:
        return False
    f=open(name, "w+")

   return f


def writelog(object):
    t=time.strftime("%Y%m%d %H:%M:%S")

    if os.path.isdir(object):
        line=t+" delete dir:"+object+"\n"

    else:
        line=t+" delete file:"+object + "\n"
    loghandel.write(line)
    loghandel.flush()
    
def closelog():
        loghandel.close()

def isdeldir(object):
    if not os.path.isdir(object):
        return False
    path, curdir = os.path.split(object)

    if not curdir:
        return False

    for eachfile in DEL_DIR :
        if eachfile == curdir:
            return True 

    return False

def isdelfile(object):
    if os.path.isfile(object):
        path, suff = os.path.splitext(object)

    else:
        return False

    if suff:
        for each in DEL_FILE :
            if each == suff:
                return True

    return False
        
def deldir(object):
    for file in os.listdir(object):
        file = os.path.join(object, file)

        if os.path.isdir(file):
            print("remove dir", file)
            os.chmod(file, stat.S_IWRITE|stat.S_IWOTH)
            deldir(file)
            writelog(file)

        elif os.path.isfile(file) :
            print("remove file", file)
            os.chmod(file, stat.S_IWRITE|stat.S_IWOTH)
            os.remove(file)
            writelog(file)
    os.rmdir(object)

#www.iplaypython.com

def delfile(object):
    if isdelfile(object):
        print("del file", object)
        try:
            os.remove(object)
        except IOError as e:
            print("error ,", e.message)
        writelog(object)


def deldirfile(object):
    if os.path.isdir(object):
        files=os.listdir(object)
        for file in files:
            file = os.path.join(object, file)
            if os.path.isdir(file):
                if isdeldir(file):
                    try:                   
                        deldir(file)
                    except IOError:
                        print ('error ', IOError.message)
                else:
                    deldirfile(file)       
            elif os.path.isfile(file):
                delfile(file)
    elif os.path.isfile(object) :       
        delfile(object)
    else:
        print ( "undefined " , object )

def main(object):
    curpath = os.curdir 
    if len(sys.argv) > 1:
        curpath = sys.argv[1]    
    try:
        print(curpath)
        global loghandel
        loghandel=creatlog(curpath)
        if not loghandel:
            print ("creat log error ", curpath)
        deldirfile(curpath)
        closelog()
    except IOError:
        print ("error is ", IOError.message)
    print ("end")


if __name__ == '__main__':
    main(sys.argv)
    if not loghandel.closed:
        loghandel.flush()
        closelog()
    

玩蛇网原创,转载请注明文章出处和来源网址:http://www.iplaypython.com/code/text-file/tf2452.html



微信公众号搜索"玩蛇网Python之家"加关注,每日最新的Python资讯、图文视频教程可以让你一手全掌握。强烈推荐关注!

微信扫描下图可直接关注

玩蛇网Python新手QQ群,欢迎加入: ① 240764603 玩蛇网Python新手群
文章发布日期:2016-04-08 15:44 玩蛇网 www.iplaypython.com

评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
相关文章推荐
别人正在看
特别推荐
去顶部去底部