python2.7刪除文件夾和刪除文件代碼實(shí)例
#!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使用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)題的,需要的朋友可以參考下2015-10-10nditer—numpy.ndarray 多維數(shù)組的迭代操作
這篇文章主要介紹了nditer—numpy.ndarray 多維數(shù)組的迭代操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-05-05Python程序設(shè)計(jì)入門(mén)(3)數(shù)組的使用
這篇文章主要介紹了Python數(shù)組的使用方法,需要的朋友可以參考下2014-06-06python中cv2.projectPoints的用法小結(jié)
這篇文章主要介紹了python中cv2.projectPoints的用法,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2023-12-12使用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-01python驗(yàn)證碼識(shí)別實(shí)例代碼
這篇文章主要介紹了python驗(yàn)證碼識(shí)別實(shí)例代碼,分享了相關(guān)代碼示例,小編覺(jué)得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-02-02