import wx, os, zipfile _selectedDir = '' _userCancel = '' class MF(wx.Frame): def __init__(self,parent,id,title): wx.Frame.__init__(self, parent, id, title, wx.DefaultPosition, (750, 500) ) vbox = wx.BoxSizer(wx.VERTICAL) self.tx = wx.TextCtrl( self, -1, '', style=wx.TE_MULTILINE ) dpanel = wx.Panel( self, -1 ) hbox = wx.BoxSizer( wx.HORIZONTAL ) dpanel.SetSizer( hbox ) self.seldir = wx.Button( dpanel, label='Select Form Directory' ) self.seldir.Bind( wx.EVT_BUTTON, self.setformdir ) self.exe = wx.Button( dpanel, label='Create Batches' ) self.exe.Bind( wx.EVT_BUTTON, self.mkbatch ) self.dir = wx.TextCtrl( dpanel, -1, str(os.getcwd()) ) hbox.Add( self.dir, 3, wx.ALIGN_CENTRE ) hbox.Add( self.seldir, 2, wx.ALIGN_CENTRE ) hbox.Add( self.exe, 2, wx.ALIGN_CENTRE ) vbox.Add( self.tx, 4, wx.EXPAND ) vbox.Add( dpanel, 0, wx.ALIGN_CENTRE ) self.SetSizer( vbox ) self.Centre() self.Bind( wx.EVT_BUTTON, self.onClose, id=wx.ID_CLOSE ) def onClose( self, event ): self.Close() def setformdir(self, ev): self.dir.SetValue( dirchoose() ) def mkbatch( self, ev ): try: flist = [] self.tx.write( 'Searching %s\n' % self.dir.GetValue() ) for dirname, subdirs, files in os.walk( self.dir.GetValue() ): for fn in [o for o in files if o.lower().endswith('.pdf')]: flist.append( (dirname,fn) ) self.tx.write( ' %s\n' % fn ) self.tx.write( '%s file(s) to batch\n\n' % len(flist) ) batch = [] i = 0 while flist: batch.append( flist.pop() ) if len(batch) == 10: i += 1 bf = os.path.join( self.dir.GetValue(), 'batch-%s.zip' % i ).encode("latin-1") arch = zipfile.ZipFile( bf, 'w', zipfile.ZIP_DEFLATED ) self.tx.write( '%s\n' % bf ) while batch: dn,fn = batch.pop() arch.write( os.path.join(dn,fn).encode("latin-1"), fn.encode("latin-1") ) self.tx.write( ' %s\n' % fn ) arch.close() if batch: i += 1 bf = os.path.join( self.dir.GetValue(), 'batch-%s.zip' % i ).encode("latin-1") arch = zipfile.ZipFile( bf, 'w', zipfile.ZIP_DEFLATED ) self.tx.write( '%s\n' % bf ) while batch: dn,fn = batch.pop() arch.write( os.path.join(dn,fn).encode("latin-1"), fn.encode("latin-1") ) self.tx.write( ' %s\n' % fn ) arch.close() except: import traceback traceback.print_exc() def dirchoose(): 'Gives the user selected path. Use: dirchoose()' global _selectedDir , _userCancel #you should define them before userPath = '.' app = wx.App() dialog = wx.DirDialog(None, "Please choose the form directory:",\ style=1 ,defaultPath=userPath, pos = (10,10)) try: if dialog.ShowModal() == wx.ID_OK: return dialog.GetPath() except: pass dialog.Destroy() return os.getcwd() class MA(wx.App): def OnInit(self): frame = MF(None,-1,'Form Batch Generator') frame.Centre() frame.Show(True) return True app = MA(0) app.MainLoop()