• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > python2.7删除文件夹和删除文件代码实例

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

作者: 字体:[增加 减小] 来源:互联网

通过本文主要向大家介绍了python2.7运行py文件,python2.7 读取文件,python2.7,python2.7显示中文,python2.7官网下载等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

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)
</div>

</div>

您可能想查找下面的文章:

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

相关文章

  • 分析并输出Python代码依赖的库的实现代码
  • Python的SQLAlchemy框架使用入门
  • python连接mysql数据库示例(做增删改操作)
  • python设计模式大全
  • python编写爬虫小程序
  • Python实现的百度站长自动URL提交小工具
  • Python 实现随机数详解及实例代码
  • Python中的字符串查找操作方法总结
  • 使用python分析git log日志示例
  • 利用Python的装饰器解决Bottle框架中用户验证问题

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • Python实现的多线程端口扫描工具分享
    • Python压缩解压缩zip文件及破解zip文件密码的方法
    • 在Django中进行用户注册和邮箱验证的方法
    • python重试装饰器示例
    • Python中为什么要用self探讨
    • 举例详解Python中threading模块的几个常用方法
    • python获取当前日期和时间的方法
    • Python中IPYTHON入门实例
    • Pythont特殊语法filter,map,reduce,apply使用方法
    • 解决python3 urllib中urlopen报错的问题

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有