python2.7刪除文件夾和刪除文件代碼實例
#!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)文章
django 實現(xiàn)將本地圖片存入數(shù)據(jù)庫,并能顯示在web上的示例
今天小編就為大家分享一篇django 實現(xiàn)將本地圖片存入數(shù)據(jù)庫,并能顯示在web上的示例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-08-08
Python使用pyppeteer進行網(wǎng)頁截圖并發(fā)送機器人實例
這篇文章主要介紹了Python使用pyppeteer進行網(wǎng)頁截圖并發(fā)送機器人實例,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-04-04
Python學(xué)習(xí)小技巧之列表項的推導(dǎo)式與過濾操作
這篇文章主要給大家介紹了Python學(xué)習(xí)小技巧之列表項的推導(dǎo)式與過濾操作的相關(guān)資料,文中介紹的非常詳細,對大家具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起看看把。2017-05-05
Python使用win32com實現(xiàn)的模擬瀏覽器功能示例
這篇文章主要介紹了Python使用win32com實現(xiàn)的模擬瀏覽器功能,結(jié)合實例形式分析了Python基于win32com模塊實現(xiàn)網(wǎng)頁的打開、登陸、加載等功能相關(guān)技巧,需要的朋友可以參考下2017-07-07
使用tensorflow實現(xiàn)VGG網(wǎng)絡(luò),訓(xùn)練mnist數(shù)據(jù)集方式
這篇文章主要介紹了使用tensorflow實現(xiàn)VGG網(wǎng)絡(luò),訓(xùn)練mnist數(shù)據(jù)集方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-05-05
pytorch之torch_scatter.scatter_max()用法
這篇文章主要介紹了pytorch之torch_scatter.scatter_max()用法,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-09-09

