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中除法使用的注意事项
Aug 21 Python
Python实现的栈(Stack)
Jan 26 Python
python自动查询12306余票并发送邮箱提醒脚本
May 21 Python
对python 操作solr索引数据的实例详解
Dec 07 Python
python基础知识(一)变量与简单数据类型详解
Apr 17 Python
利用ImageAI库只需几行python代码实现目标检测
Aug 09 Python
python爬虫-模拟微博登录功能
Sep 12 Python
Python基于内置库pytesseract实现图片验证码识别功能
Feb 24 Python
Pyspark读取parquet数据过程解析
Mar 27 Python
利用jupyter网页版本进行python函数查询方式
Apr 14 Python
Pytorch使用PIL和Numpy将单张图片转为Pytorch张量方式
May 25 Python
8种常用的Python工具
Aug 05 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
Php做的端口嗅探器--可以指定网站和端口
2006/10/09 PHP
解析php利用正则表达式解决采集内容排版的问题
2013/06/20 PHP
thinkphp 一个页面使用2次分页的实现方法
2013/07/15 PHP
php分页代码学习示例分享
2014/02/20 PHP
php+ajax导入大数据时产生的问题处理
2014/06/11 PHP
PHP中feof()函数实例测试
2014/08/23 PHP
PHP面向对象程序设计之对象生成方法详解
2016/12/02 PHP
PHP实现UTF8二进制及明文字符串的转化功能示例
2017/11/20 PHP
经典的解除许多网站无法复制文字的绝招
2006/12/31 Javascript
JS 页面内容搜索,类似于 Ctrl+F功能的实现代码
2007/08/13 Javascript
javascript web页面刷新的方法收集
2009/07/02 Javascript
解析使用JS 清空File控件的路径值
2013/07/08 Javascript
JS如何将UTC格式时间转本地格式
2013/09/04 Javascript
js AppendChild与insertBefore用法详细对比
2013/12/16 Javascript
JS实现的车标图片提示效果代码
2015/10/10 Javascript
NodeJS模块Buffer原理及使用方法解析
2020/11/11 NodeJs
[02:40]DOTA2殁境神蚀者 英雄基础教程
2013/11/26 DOTA
使用Python来编写HTTP服务器的超级指南
2016/02/18 Python
python编写简单爬虫资料汇总
2016/03/22 Python
Python简单实现两个任意字符串乘积的方法示例
2018/04/12 Python
Django实现表单验证
2018/09/08 Python
Python3 安装PyQt5及exe打包图文教程
2019/01/08 Python
python3人脸识别的两种方法
2019/04/25 Python
python SVD压缩图像的实现代码
2019/11/05 Python
vue常用指令代码实例总结
2020/03/16 Python
解决Python spyder显示不全df列和行的问题
2020/04/20 Python
检察官就职演讲稿
2014/01/13 职场文书
《影子》教学反思
2014/02/21 职场文书
银行贷款收入证明
2014/10/17 职场文书
领导欢迎词范文
2015/01/26 职场文书
保卫工作个人总结
2015/03/03 职场文书
门卫管理制度范本
2015/08/05 职场文书
古诗文之爱国名句(77句)
2019/09/24 职场文书
聊聊Python String型列表求最值的问题
2022/01/18 Python
【海涛dota解说】DCG联赛第一周 LGD VS DH
2022/04/01 DOTA
Win11安全功能升级:内置防网络钓鱼功能
2022/04/08 数码科技