欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python2.7刪除文件夾和刪除文件代碼實(shí)例

 更新時(shí)間:2013年12月18日 09:10:17   作者:  
python刪除文件夾t和刪除文件代碼實(shí)例,大家參考使用吧,運(yùn)行環(huán)境是python2.7

復(fù)制代碼 代碼如下:

#!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)

相關(guān)文章

  • python 日志 logging模塊詳細(xì)解析

    python 日志 logging模塊詳細(xì)解析

    這篇文章主要介紹了python 日志 logging模塊 詳細(xì)解析,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的工作或?qū)W習(xí)具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-03-03
  • Python使用Qt5實(shí)現(xiàn)水平導(dǎo)航欄的示例代碼

    Python使用Qt5實(shí)現(xiàn)水平導(dǎo)航欄的示例代碼

    本文主要介紹了Python使用Qt5實(shí)現(xiàn)水平導(dǎo)航欄的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-03-03
  • 在arcgis使用python腳本進(jìn)行字段計(jì)算時(shí)是如何解決中文問(wèn)題的

    在arcgis使用python腳本進(jìn)行字段計(jì)算時(shí)是如何解決中文問(wèn)題的

    這篇文章主要介紹了在arcgis使用python腳本進(jìn)行字段計(jì)算時(shí)是如何解決中文問(wèn)題的,需要的朋友可以參考下
    2015-10-10
  • nditer—numpy.ndarray 多維數(shù)組的迭代操作

    nditer—numpy.ndarray 多維數(shù)組的迭代操作

    這篇文章主要介紹了nditer—numpy.ndarray 多維數(shù)組的迭代操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2021-05-05
  • Python程序設(shè)計(jì)入門(mén)(3)數(shù)組的使用

    Python程序設(shè)計(jì)入門(mén)(3)數(shù)組的使用

    這篇文章主要介紹了Python數(shù)組的使用方法,需要的朋友可以參考下
    2014-06-06
  • 怎么解決pycharm license Acti的方法

    怎么解決pycharm license Acti的方法

    這篇文章主要介紹了怎么解決pycharm license Acti的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-10-10
  • python中cv2.projectPoints的用法小結(jié)

    python中cv2.projectPoints的用法小結(jié)

    這篇文章主要介紹了python中cv2.projectPoints的用法,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2023-12-12
  • Python幫你微信頭像任意添加裝飾別再@微信官方了

    Python幫你微信頭像任意添加裝飾別再@微信官方了

    昨天朋友圈刷爆了@微信官方的梗,從起初的為頭像添加國(guó)旗,到最后的各種Book思議的需求…而我呢?下面跟隨小編一起學(xué)習(xí)Python幫你微信頭像任意添加裝飾別再@微信官方了,感興趣的朋友一起看看吧
    2019-09-09
  • 使用numpngw和matplotlib生成png動(dòng)畫(huà)的示例代碼

    使用numpngw和matplotlib生成png動(dòng)畫(huà)的示例代碼

    這篇文章主要介紹了使用numpngw和matplotlib生成png動(dòng)畫(huà)的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • python驗(yàn)證碼識(shí)別實(shí)例代碼

    python驗(yàn)證碼識(shí)別實(shí)例代碼

    這篇文章主要介紹了python驗(yàn)證碼識(shí)別實(shí)例代碼,分享了相關(guān)代碼示例,小編覺(jué)得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下
    2018-02-02

最新評(píng)論