代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Python
】
批量删除所下载的.git文件夹
作者:
hallyhong
/ 发布于
2012/10/22
/
777
<div>!/usr/bin/env python</div> <div></div> <div>title = 'this is a test for del test directorys'</div> <div></div> import os, string, sys, time, re, math, fileinput, glob, shutil, stat</div> <div></div> <div>log_name = 'delGitFile'</div> <div>log_ext = '.log'</div> <div></div> <div>DEL_DIR=['.git']</div> <div>DEL_FILE=['.gitignore']</div> <div></div> <div>def creatlog(object):</div> <div> if os.path.isdir(object):</div> <div> name = object + "/" + log_name + time.strftime(" %Y%m%d %H_%M_%S") + log_ext</div> <div> print("Creat LogFile: " + log_name + time.strftime(" %Y%m%d %H_%M_%S") + log_ext)</div> <div> else:</div> <div> return False</div> <div> f=open(name, "w+")</div> <div> return f</div> <div></div> <div></div> <div>def writelog(object):</div> <div> print "writelog() - "+object</div> <div> t=time.strftime("%Y%m%d %H:%M:%S")</div> <div> if os.path.isdir(object):</div> <div> print "writelog() - dir: "+object</div> <div> line=t+" delete dir:"+object+"\n"</div> <div> else:</div> <div> print "writelog() - file: "+object</div> <div> line=t+" delete file:"+object + "\n"</div> <div> loghandel.write(line)</div> <div> loghandel.flush()</div> <div> </div> <div>def closelog():</div> <div> loghandel.close()</div> <div></div> <div>def isdeldir(object):</div> <div> if not os.path.isdir(object):</div> <div> return False</div> <div> path, curdir = os.path.split(object)</div> <div> if not curdir:</div> <div> return False</div> <div> for eachfile in DEL_DIR :</div> <div> if eachfile == curdir:</div> <div> return True</div> <div> return False</div> <div></div> <div>def isdelfile(object):</div> <div> if os.path.isfile(object):</div> <div> path, suff = os.path.split(object)</div> <div> else:</div> <div> return False</div> <div></div> <div> if suff:</div> <div> for each in DEL_FILE :</div> <div> if each == suff:</div> <div> return True</div> <div> else:</div> <div> return False</div> <div> else:</div> <div> return False</div> <div></div> <div> </div> <div>def deldir(object):</div> <div> for file in os.listdir(object):</div> <div> file = os.path.join(object, file)</div> <div> if os.path.isdir(file):</div> <div> print("remove dir", file)</div> <div> os.chmod(file, stat.S_IWRITE|stat.S_IWOTH)</div> <div> deldir(file)</div> <div> elif os.path.isfile(file) :</div> <div> print("remove file", file)</div> <div> os.chmod(file, stat.S_IWRITE|stat.S_IWOTH)</div> <div> os.remove(file)</div> <div> os.rmdir(object)</div> <div></div> <div></div> <div>def delfile(object):</div> <div> if isdelfile(object):</div> <div> writelog(object)</div> <div> try:</div> <div> os.remove(object)</div> <div> except IOError as e:</div> <div> print("error ,", e.message)</div> <div></div> <div></div> <div>def deldirfile(object):</div> <div> if os.path.isdir(object):</div> <div> files=os.listdir(object)</div> <div> for file in files:</div> <div> file = os.path.join(object, file)</div> <div> if os.path.isdir(file):</div> <div> if isdeldir(file):</div> <div> print("5 deldirfile()-"+file)</div> <div> writelog(file)</div> <div> try:</div> <div> shutil.rmtree(file)</div> <div> except IOError:</div> <div> print ('error ', IOError.message)</div> <div> else:</div> <div> deldirfile(file) </div> <div> elif os.path.isfile(file):</div> <div> delfile(file)</div> <div> elif os.path.isfile(object) : </div> <div> delfile(object)</div> <div> else:</div> <div> print ( "undefined " , object )</div> <div></div> <div></div> <div>def main(object):</div> <div> curpath = os.curdir</div> <div> if len(sys.argv) > 1:</div> <div> curpath = sys.argv[1] </div> <div> try:</div> <div> print("the root dir is "+curpath)</div> <div> global loghandel</div> <div> loghandel=creatlog(curpath)</div> <div> if not loghandel:</div> <div> print ("creat log error ", curpath)</div> <div> deldirfile(curpath)</div> <div> closelog()</div> <div> except IOError:</div> <div> print ("error is ", IOError.message)</div> <div> print ("end")</div> <div></div> <div></div> if __name__ == '__main__':</div> <div> main(sys.argv)</div> <div> if not loghandel.closed:</div> <div> loghandel.flush()</div> <div> closelog()</div> <div> </div> <div></div>
试试其它关键字
批量删除
同语言下
.
比较两个图片的相似度
.
过urllib2获取带有中文参数的url内容
.
不下载获取远程图片的宽度和高度及文件大小
.
通过qrcode库生成二维码
.
通过httplib发送GET和POST请求
.
Django下解决小文件下载
.
遍历windows的所有窗口并输出窗口标题
.
根据窗口标题调用窗口
.
python 抓取搜狗指定公众号
.
pandas读取指定列
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
hallyhong
贡献的其它代码
(
1
)
.
批量删除所下载的.git文件夹
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3