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)文章
django 實(shí)現(xiàn)將本地圖片存入數(shù)據(jù)庫(kù),并能顯示在web上的示例
今天小編就為大家分享一篇django 實(shí)現(xiàn)將本地圖片存入數(shù)據(jù)庫(kù),并能顯示在web上的示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-08-08
win10從零安裝配置pytorch全過(guò)程圖文詳解
這篇文章主要介紹了win10從零安裝配置pytorch全過(guò)程,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05
Python使用pyppeteer進(jìn)行網(wǎng)頁(yè)截圖并發(fā)送機(jī)器人實(shí)例
這篇文章主要介紹了Python使用pyppeteer進(jìn)行網(wǎng)頁(yè)截圖并發(fā)送機(jī)器人實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-04-04
刪除pycharm鼠標(biāo)右鍵快捷鍵打開(kāi)項(xiàng)目的操作
這篇文章主要介紹了刪除pycharm鼠標(biāo)右鍵快捷鍵打開(kāi)項(xiàng)目的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-01-01
Python學(xué)習(xí)小技巧之列表項(xiàng)的推導(dǎo)式與過(guò)濾操作
這篇文章主要給大家介紹了Python學(xué)習(xí)小技巧之列表項(xiàng)的推導(dǎo)式與過(guò)濾操作的相關(guān)資料,文中介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看把。2017-05-05
Python使用win32com實(shí)現(xiàn)的模擬瀏覽器功能示例
這篇文章主要介紹了Python使用win32com實(shí)現(xiàn)的模擬瀏覽器功能,結(jié)合實(shí)例形式分析了Python基于win32com模塊實(shí)現(xiàn)網(wǎng)頁(yè)的打開(kāi)、登陸、加載等功能相關(guān)技巧,需要的朋友可以參考下2017-07-07
python輸出國(guó)際象棋棋盤(pán)的實(shí)例分享
在本篇文章里小編給大家整理的是一篇關(guān)于python輸出國(guó)際象棋棋盤(pán)的實(shí)例詳解,有興趣的朋友們可以參考下。2020-11-11
使用tensorflow實(shí)現(xiàn)VGG網(wǎng)絡(luò),訓(xùn)練mnist數(shù)據(jù)集方式
這篇文章主要介紹了使用tensorflow實(shí)現(xiàn)VGG網(wǎng)絡(luò),訓(xùn)練mnist數(shù)據(jù)集方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-05-05
pytorch之torch_scatter.scatter_max()用法
這篇文章主要介紹了pytorch之torch_scatter.scatter_max()用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09

