Lagde en GUI versjon bare for gøy,nytteverdien kan diskuters.
Teller filer rimlig raskt,leser kun 1 og 1 fil inn til minnet.
Wxpython for GUI pakket i Gui2exe(komprimert UPX) Inno-setup for windows installasjon.
Poster koden så det kommer kansje noe nyttig ut av dette,viss noen lurer på GUI i python.
Både PyQt og PyGTK er bra GUI toolkit,men jeg finner wxpython mest pythonic og nærmest det jeg liker med python.
Kode
import wx
import os
class MediaCounter(wx.Frame):
'''Count media files in chosen folder(include subfolder)'''
def __init__(self, parent, mytitle, mysize):
wx.Frame.__init__(self, parent, wx.ID_ANY, mytitle, size=mysize, style=wx.DEFAULT_DIALOG_STYLE | wx.MINIMIZE_BOX)
#Setup(panel so it look good on all platforms)
self.SetBackgroundColour("#d8d8bf")
self.panel = wx.Panel(self)
ico = wx.Icon('m.ico', wx.BITMAP_TYPE_ICO)
self.SetIcon(ico)
#--| Text
self.mp3 = wx.StaticText(self.panel,-1,'<Your Folder>',(25,90),(100,25),wx.ST_NO_AUTORESIZE )
self.count_text = wx.StaticText(self.panel,-1,'<File count>',(25,150),(200,25),wx.ST_NO_AUTORESIZE )
wx.StaticText(self.panel,-1,'Chose file extension',(205,60)).SetForegroundColour('blue')
wx.StaticText(self.panel,-1,'------------------------------',(200,75))
#--| Buttons
choose_folder = wx.Button(self.panel,-1,"Search Location",(20,60),(90,25))
search = wx.Button(self.panel,-1,"Search",(20,120),(90,25))
self.Bind(wx.EVT_BUTTON,self.OnFiledialog, choose_folder)
self.Bind(wx.EVT_BUTTON,self.OnSearch, search)
#--| CheckBox
self.cb1 = wx.CheckBox(self.panel,-1,"Mp3",(200,85),(40,20))
self.cb2 = wx.CheckBox(self.panel,-1,"Avi",(240,85),(40,20))
self.cb3 = wx.CheckBox(self.panel,-1,"Mpg",(280,85),(40,20))
self.cb4 = wx.CheckBox(self.panel,-1,"Mov",(200,110),(40,20))
self.cb5 = wx.CheckBox(self.panel,-1,"Wav",(240,110),(40,20))
self.cb6 = wx.CheckBox(self.panel,-1,"Bmp",(280,110),(40,20))
self.cb7 = wx.CheckBox(self.panel,-1,"Jpg",(200,135),(40,20))
self.cb8 = wx.CheckBox(self.panel,-1,"Png",(240,135),(40,20))
self.cb9 = wx.CheckBox(self.panel,-1,"Gif",(280,135),(40,20))
self.Bind(wx.EVT_CHECKBOX,self.onChoice, self.cb1)
self.Bind(wx.EVT_CHECKBOX,self.onChoice, self.cb2)
self.Bind(wx.EVT_CHECKBOX,self.onChoice, self.cb3)
self.Bind(wx.EVT_CHECKBOX,self.onChoice, self.cb4)
self.Bind(wx.EVT_CHECKBOX,self.onChoice, self.cb5)
self.Bind(wx.EVT_CHECKBOX,self.onChoice, self.cb6)
self.Bind(wx.EVT_CHECKBOX,self.onChoice, self.cb7)
self.Bind(wx.EVT_CHECKBOX,self.onChoice, self.cb8)
self.Bind(wx.EVT_CHECKBOX,self.onChoice, self.cb9)
#---| Picture
wx.StaticBitmap(self.panel,-1,wx.Bitmap("head2.png",
wx.BITMAP_TYPE_ANY),pos=(20,10), size=(300,40))
wx.StaticBitmap(self.panel,-1,wx.Bitmap("mp3_48.png",
wx.BITMAP_TYPE_ANY),pos=(25,190), size=(48,48))
wx.StaticBitmap(self.panel,-1,wx.Bitmap("avi_48.png",
wx.BITMAP_TYPE_ANY),pos=(140,190), size=(48,48))
wx.StaticBitmap(self.panel,-1,wx.Bitmap("jpg_48.png",
wx.BITMAP_TYPE_ANY),pos=(260,190), size=(48,48))
def OnFiledialog(self,evt):
'''Choose folder for search'''
dlg = wx.wx.DirDialog(self,"Choose a directory:",style=wx.DD_DEFAULT_STYLE)
if dlg.ShowModal() == wx.ID_OK:
path = None
self.path = dlg.GetPath()
self.mp3.SetLabel(self.path)
dlg.Destroy()
def onChoice(self,event):
'''Select File extension'''
d = {self.cb1.IsChecked(): 'mp3', self.cb2.IsChecked(): 'avi',
self.cb3.IsChecked(): 'mpg', self.cb4.IsChecked(): 'mov',
self.cb5.IsChecked(): 'wav', self.cb6.IsChecked(): 'bmp',
self.cb7.IsChecked(): 'jpg', self.cb8.IsChecked(): 'png',
self.cb9.IsChecked(): 'gif'}
if True in d:
self.ext_data = tuple(d[True])
def OnSearch(self,evt):
'''Search chosen folder'''
counter = 0
try:
for root, dirs, files in os.walk(self.path):
for f in files:
filename = os.path.join(root, f)
if filename.endswith(self.ext_data):
counter += 1
self.count_text.SetLabel(str(counter))
except AttributeError:
self.count_text.SetLabel('Choose location and file type')
if __name__ == "__main__":
app = wx.App()
mytitle = 'Mediafile counter'
width = 350
height = 300
MediaCounter(None, mytitle, (width, height)).Show()
app.MainLoop()