[python 3.1]删除指定目录下的bin,obj文件夹
Code
#-------------------------------------------------------------------------------
# Name: bincleaner.py
# Purpose: 删除某指定目录下的bin,obj文件夹
#
# Author: rdu
#
# Created: 26/08/2009
# Copyright: (c) rdu 2009
# Licence: <your licence>
#-------------------------------------------------------------------------------
#!/usr/bin/env python
from tkinter import *
from tkinter import ttk
from tkinter import filedialog
import sys
import os
import stat
import time
ERROR_STR= """Error removing %(path)s"""
def rmgeneric(path, __func__):
try:
__func__(path)
msg.set('Removed ' + path)
lblmsg.update_idletasks()
time.sleep(.1)
except:
msg.set( ERROR_STR % {'path' : path})
# delete a dir at @path
# the dir does not need to be empty
# the dir itself is also deleted
def deletedir(path):
if not os.path.isdir(path):
return
files=os.listdir(path)
for x in files:
fullpath=os.path.join(path, x)
if os.path.isfile(fullpath):
f=os.remove
rmgeneric(fullpath, f)
elif os.path.isdir(fullpath):
deletedir(fullpath)
f=os.rmdir
rmgeneric(fullpath, f)
f=os.rmdir
rmgeneric(path, f)
def getdestfolder():
folder = filedialog.askdirectory(initialdir=destfolder.get())
if folder:
destfolder.set(folder)
def deep(path):
for item in os.listdir(path):
subpath=os.path.join(path, item)
mode=os.stat(subpath)[stat.ST_MODE]
if stat.S_ISDIR(mode):
if item.upper() in ['BIN','OBJ']:
lbxstatus.insert(END,subpath.replace('/','\\'))
lbxstatus.update_idletasks()
lbxstatus.see(END)
else:
deep(subpath)
def findall():
path=destfolder.get()
if path:
lbxstatus.delete(0,END)
deep(path)
# Colorize alternating lines of the listbox
for i in range(0,lbxstatus.size(),2):
lbxstatus.itemconfigure(i, background='#f0f0ff')
msg.set('Done')
def deleteselected():
idxs = lbxstatus.curselection()
if len(idxs) > 0:
for idx in idxs:
deletedir(lbxstatus.get(idx))
findall()
def deleteall():
for idx in range(lbxstatus.size()):
deletedir(lbxstatus.get(idx))
findall()
root=Tk()
root.minsize(600,400)
# Frame for designating/displaying destination folder
destframe=ttk.Frame(root)
destframe.pack(side=TOP,fill=X)
destfolder=StringVar() # String var to keep the current dest folder setting
btndest=ttk.Button(destframe,text='From here',command=getdestfolder)
btndest.pack(side=LEFT)
lbldest=ttk.Label(destframe,textvariable=destfolder,relief=SUNKEN)
lbldest.pack(side=LEFT,fill=X,expand=1)
# Frame for action buttons
actionframe=ttk.Frame(root)
actionframe.pack(side=TOP,fill=X)
btnfindall=ttk.Button(actionframe,text='Find All',command=findall)
btnfindall.pack(side=LEFT)
btndeleteall=ttk.Button(actionframe,text='Delete All',command=deleteall)
btndeleteall.pack(side=LEFT)
btndeleteselected=ttk.Button(actionframe,text='Delete Selected',command=deleteselected)
btndeleteselected.pack(side=LEFT)
# Frame for displaying status info
statusframe=ttk.Frame(root)
statusframe.pack(side=TOP,fill=BOTH,expand=1)
statusframe.grid_columnconfigure(0, weight=1)
statusframe.grid_rowconfigure(0, weight=1)
msg=StringVar()
lbxstatus=Listbox(statusframe,selectmode='extended')
lbxstatus.grid(column=0,row=0,sticky=(N,W,E,S))
vscroll = ttk.Scrollbar( statusframe, orient=VERTICAL, command=lbxstatus.yview)
vscroll.grid(column=1,row=0,sticky=(S,N))
lbxstatus['yscrollcommand']=vscroll.set
lblmsg=ttk.Label(statusframe,textvariable=msg,relief=SUNKEN)
lblmsg.grid(column=0,row=1,columnspan=2,sticky=(W,E))
destfolder.set("D:\\VS2008Projs")
root.title("Bin/Debug Folders Cleaner.")
root.mainloop()
#-------------------------------------------------------------------------------
# Name: bincleaner.py
# Purpose: 删除某指定目录下的bin,obj文件夹
#
# Author: rdu
#
# Created: 26/08/2009
# Copyright: (c) rdu 2009
# Licence: <your licence>
#-------------------------------------------------------------------------------
#!/usr/bin/env python
from tkinter import *
from tkinter import ttk
from tkinter import filedialog
import sys
import os
import stat
import time
ERROR_STR= """Error removing %(path)s"""
def rmgeneric(path, __func__):
try:
__func__(path)
msg.set('Removed ' + path)
lblmsg.update_idletasks()
time.sleep(.1)
except:
msg.set( ERROR_STR % {'path' : path})
# delete a dir at @path
# the dir does not need to be empty
# the dir itself is also deleted
def deletedir(path):
if not os.path.isdir(path):
return
files=os.listdir(path)
for x in files:
fullpath=os.path.join(path, x)
if os.path.isfile(fullpath):
f=os.remove
rmgeneric(fullpath, f)
elif os.path.isdir(fullpath):
deletedir(fullpath)
f=os.rmdir
rmgeneric(fullpath, f)
f=os.rmdir
rmgeneric(path, f)
def getdestfolder():
folder = filedialog.askdirectory(initialdir=destfolder.get())
if folder:
destfolder.set(folder)
def deep(path):
for item in os.listdir(path):
subpath=os.path.join(path, item)
mode=os.stat(subpath)[stat.ST_MODE]
if stat.S_ISDIR(mode):
if item.upper() in ['BIN','OBJ']:
lbxstatus.insert(END,subpath.replace('/','\\'))
lbxstatus.update_idletasks()
lbxstatus.see(END)
else:
deep(subpath)
def findall():
path=destfolder.get()
if path:
lbxstatus.delete(0,END)
deep(path)
# Colorize alternating lines of the listbox
for i in range(0,lbxstatus.size(),2):
lbxstatus.itemconfigure(i, background='#f0f0ff')
msg.set('Done')
def deleteselected():
idxs = lbxstatus.curselection()
if len(idxs) > 0:
for idx in idxs:
deletedir(lbxstatus.get(idx))
findall()
def deleteall():
for idx in range(lbxstatus.size()):
deletedir(lbxstatus.get(idx))
findall()
root=Tk()
root.minsize(600,400)
# Frame for designating/displaying destination folder
destframe=ttk.Frame(root)
destframe.pack(side=TOP,fill=X)
destfolder=StringVar() # String var to keep the current dest folder setting
btndest=ttk.Button(destframe,text='From here',command=getdestfolder)
btndest.pack(side=LEFT)
lbldest=ttk.Label(destframe,textvariable=destfolder,relief=SUNKEN)
lbldest.pack(side=LEFT,fill=X,expand=1)
# Frame for action buttons
actionframe=ttk.Frame(root)
actionframe.pack(side=TOP,fill=X)
btnfindall=ttk.Button(actionframe,text='Find All',command=findall)
btnfindall.pack(side=LEFT)
btndeleteall=ttk.Button(actionframe,text='Delete All',command=deleteall)
btndeleteall.pack(side=LEFT)
btndeleteselected=ttk.Button(actionframe,text='Delete Selected',command=deleteselected)
btndeleteselected.pack(side=LEFT)
# Frame for displaying status info
statusframe=ttk.Frame(root)
statusframe.pack(side=TOP,fill=BOTH,expand=1)
statusframe.grid_columnconfigure(0, weight=1)
statusframe.grid_rowconfigure(0, weight=1)
msg=StringVar()
lbxstatus=Listbox(statusframe,selectmode='extended')
lbxstatus.grid(column=0,row=0,sticky=(N,W,E,S))
vscroll = ttk.Scrollbar( statusframe, orient=VERTICAL, command=lbxstatus.yview)
vscroll.grid(column=1,row=0,sticky=(S,N))
lbxstatus['yscrollcommand']=vscroll.set
lblmsg=ttk.Label(statusframe,textvariable=msg,relief=SUNKEN)
lblmsg.grid(column=0,row=1,columnspan=2,sticky=(W,E))
destfolder.set("D:\\VS2008Projs")
root.title("Bin/Debug Folders Cleaner.")
root.mainloop()