• 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中的reduce内建函数使用方法指南
  • python在不同层级目录import模块的方法
  • 深入讨论Python函数的参数的默认值所引发的问题的原因
  • python中 ? : 三元表达式的使用介绍
  • 详细解析Python中的变量的数据类型
  • python获取beautifulphoto随机某图片代码实例
  • Windows下Python的Django框架环境部署及应用编写入门
  • 使用Python中的cookielib模拟登录网站
  • Python常用模块用法分析
  • 零基础写python爬虫之爬虫编写全记录

文章分类

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

最近更新的内容

    • Python中用format函数格式化字符串的用法
    • Python批量修改文本文件内容的方法
    • Python操作sqlite3快速、安全插入数据(防注入)的实例
    • 用Python实现一个简单的线程池
    • Python HTMLParser模块解析html获取url实例
    • 常见的在Python中实现单例模式的三种方法
    • Python兔子毒药问题实例分析
    • python制作花瓣网美女图片爬虫
    • Python判断直线和矩形是否相交的方法
    • python采集博客中上传的QQ截图文件

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

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