python2.7删除文件夹和删除文件代码实例


Posted in Python onDecember 18, 2013
#!c:\python27\python.exe
# -*- coding: utf-8 -*-
import os
import re
from os import path
from shutil import rmtree
DEL_DIRS = None
DEL_FILES = r'(.+?\.pyc$|.+?\.pyo$|.+?\.log$)'
def del_dir(p):
    """Delete a directory."""
    if path.isdir(p):
        rmtree(p)
        print('D : %s' % p)
def del_file(p):
    """Delete a file."""
    if path.isfile(p):
        os.remove(p)
        print('F : %s' % p)
def gen_deletions(directory, del_dirs=DEL_DIRS, del_files=DEL_FILES):
    """Generate deletions."""
    patt_dirs = None if del_dirs == None else re.compile(del_dirs)
    patt_files = None if del_files == None else re.compile(del_files)
    for root, dirs, files in os.walk(directory):
        if patt_dirs:
            for d in dirs:
                if patt_dirs.match(d):
                    yield path.join(root, d)
        if patt_files:
            for f in files:
                 if patt_files.match(f):
                    yield path.join(root, f)
def confirm_deletions(directory):
    import Tkinter
    import tkMessageBox
    root = Tkinter.Tk()
    root.withdraw()
    res = tkMessageBox.askokcancel("Confirm deletions?",
        "Do you really wish to delete?\n\n"
        "Working directory:\n%s\n\n"
        "Delete conditions:\n(D)%s\n(F)%s"
        % (directory, DEL_DIRS, DEL_FILES))
    if res:
        print('Processing...')
        m, n = 0, 0
        for p in gen_deletions(directory):
            if path.isdir(p):
                del_dir(p)
                m += 1
            elif path.isfile(p):
                del_file(p)
                n += 1
        print('Clean %d dirs and %d files.' % (m, n))
        root.destroy()
    else:
        print('Canceled.')
        root.destroy()
    root.mainloop()
if __name__ == '__main__':
    import sys
    argv = sys.argv
    directory = argv[1] if len(argv) >= 2 else os.getcwd()
    confirm_deletions(directory)
    # import subprocess
    # subprocess.call("pause", shell=True)
Python 相关文章推荐
Python之PyUnit单元测试实例
Oct 11 Python
python文件的md5加密方法
Apr 06 Python
Python备份目录及目录下的全部内容的实现方法
Jun 12 Python
python使用SMTP发送qq或sina邮件
Oct 21 Python
Python学生信息管理系统修改版
Mar 13 Python
python for循环remove同一个list过程解析
Aug 14 Python
matlab中imadjust函数的作用及应用举例
Feb 27 Python
Python字符串hashlib加密模块使用案例
Mar 10 Python
python requests.get带header
May 05 Python
pandas apply多线程实现代码
Aug 17 Python
Python基于tkinter canvas实现图片裁剪功能
Nov 05 Python
Python __slots__的使用方法
Nov 15 Python
python使用xmlrpc实例讲解
Dec 17 #Python
python三元运算符实现方法
Dec 17 #Python
用python写asp详细讲解
Dec 16 #Python
python模块restful使用方法实例
Dec 10 #Python
python解析模块(ConfigParser)使用方法
Dec 10 #Python
python基础入门详解(文件输入/输出 内建类型 字典操作使用方法)
Dec 08 #Python
python socket网络编程步骤详解(socket套接字使用)
Dec 06 #Python
You might like
yii2中使用Active Record模式的方法
2016/01/09 PHP
PHP微信开发之查询微信精选文章
2016/06/23 PHP
IE8 原生JSON支持
2009/04/13 Javascript
JQuery 选择器 xpath 语法应用
2010/05/13 Javascript
基于Jquery的将DropDownlist的选中值赋给label的实现代码
2011/05/06 Javascript
使用JavaScript获取电池状态的方法
2014/05/03 Javascript
node.js中的fs.appendFile方法使用说明
2014/12/17 Javascript
javascript中Array数组的迭代方法实例分析
2015/02/04 Javascript
详解JavaScript操作HTML DOM的基本方式
2015/10/21 Javascript
window.setInterval()方法的定义和用法及offsetLeft与style.left的区别
2015/11/11 Javascript
从零学习node.js之搭建http服务器(二)
2017/02/21 Javascript
jQuery使用DataTable实现删除数据后重新加载功能
2017/02/27 Javascript
JavaScript中双向数据绑定详解
2017/05/03 Javascript
JS奇技之利用scroll来监听resize详解
2017/06/15 Javascript
vue resource post请求时遇到的坑
2017/10/19 Javascript
jQuery实现轮播图及其原理详解
2020/04/12 jQuery
vue 微信扫码登录(自定义样式)
2020/01/06 Javascript
[00:08]DOTA2勇士令状等级奖励“天外飞星”
2019/05/24 DOTA
CentOS安装pillow报错的解决方法
2016/01/27 Python
详解使用pymysql在python中对mysql的增删改查操作(综合)
2017/01/18 Python
python使用matplotlib绘图时图例显示问题的解决
2017/04/27 Python
简单实现python聊天程序
2018/04/01 Python
Python玩转PDF的各种骚操作
2019/05/06 Python
Golang GBK转UTF-8的例子
2019/08/26 Python
Python selenium页面加载慢超时的解决方案
2020/03/18 Python
Windows10+anacond+GPU+pytorch安装详细过程
2020/03/24 Python
python如何编写win程序
2020/06/08 Python
python 字符串的驻留机制及优缺点
2020/06/19 Python
英国领先的汽车轮胎和快速健康中心:Kwik Fit
2017/10/29 全球购物
公务员职业生涯规划书范文  
2014/01/19 职场文书
员工三分钟演讲稿
2014/08/19 职场文书
缓刑人员思想汇报500字
2014/09/12 职场文书
教师年度个人总结
2015/02/11 职场文书
告知书格式
2015/07/01 职场文书
航班延误投诉信
2015/07/02 职场文书
什么是求职信?求职信应包含哪些内容?
2019/08/14 职场文书