批量删除所下载的.git文件夹,批量删除.git文件夹,因经常需要将androi


因经常需要将android项目打包给其他客户做第三方应用开发,但由于.git目录的存在导致打包出来的文件很大,所以想删掉这些目录及相关文件,减小压缩包的文件大小,因此编写了这个脚本来作此动作。功能比较简单,但还是稍稍提高了一下工作效率。

#!/usr/bin/env pythontitle = 'this is a test for del test directorys'import os, string, sys, time, re, math, fileinput, glob, shutil, statlog_name = 'delGitFile'log_ext = '.log'DEL_DIR=['.git']DEL_FILE=['.gitignore']def creatlog(object):    if os.path.isdir(object):        name = object + "/" + log_name + time.strftime(" %Y%m%d %H_%M_%S") + log_ext        print("Creat LogFile: " + log_name + time.strftime(" %Y%m%d %H_%M_%S") + log_ext)    else:        return False    f=open(name, "w+")    return fdef writelog(object):    print "writelog() - "+object    t=time.strftime("%Y%m%d %H:%M:%S")    if os.path.isdir(object):        print "writelog() - dir: "+object        line=t+" delete dir:"+object+"\\n"    else:        print "writelog() - file: "+object        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 Falsedef isdelfile(object):    if os.path.isfile(object):        path, suff = os.path.split(object)    else:        return False    if suff:        for each in DEL_FILE :            if each == suff:                return True            else:                return False    else:        return Falsedef 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)        elif os.path.isfile(file) :            print("remove file", file)            os.chmod(file, stat.S_IWRITE|stat.S_IWOTH)            os.remove(file)    os.rmdir(object)def delfile(object):    if isdelfile(object):        writelog(object)        try:            os.remove(object)        except IOError as e:            print("error ,", e.message)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):                    print("5 deldirfile()-"+file)                    writelog(file)                    try:                        shutil.rmtree(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("the root dir is "+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://byrx.net

评论关闭