批量删除所下载的.git文件夹,批量删除.git文件夹,批量删除.git和.gi
批量删除所下载的.git文件夹,批量删除.git文件夹,批量删除.git和.gi
批量删除.git和.gitignore
#!/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()
相关内容
- 得到N以内的所有的质数,得到N质数,getPrime.pyi
- peewee的简单封装,peewee简单封装,[Python]代码im
- python 生成IP段,python生成ip,[Python]代码#!
- Python 扫描IP段 指定端口是否开放,pythonip,TCP21.py#!/u
- 使用python Tk的实现tablepanel+treeview,pythontablepanel,window
- python BeautifulSoup 抓取网页内指定内容,,# _*_ coding
- 获得本机硬件信息,获得机硬件信息,注意:这段代码需
- Python 对文件通配符匹配,python通配符匹配,有时候,我
- 爱的方程式(3D),方程式3d,from mpl_too
- python 实现简单的SYSLOG服务器,并写入MYSQL数据库,sys
评论关闭