请教文件压缩问题
lvxinzhi
|
1#
lvxinzhi 发表于 2007-08-10 09:45
请教文件压缩问题
def bkedi(self,sours_parth,d_path):
target_file = time.strftime('%Y%m%d%H%M%S') + '.zip' #myZipFile = zipfile.ZipFile(target_file, 'w' )# 压缩所有文件,包含子目录 tmpZipFile = zipfile.ZipFile(target_file,'w') #source_dir = sours_parth source_dir = r'C:\MH' #print source_dir for root,dirs,files in os.walk(source_dir): for vfileName in files: fileName = os.path.join(root,vfileName) tmpZipFile.write( fileName, fileName, zipfile.ZIP_DEFLATED ) # 压缩完成 tmpZipFile.close() 上面是压缩代码,为什么source_dir 用传进来的地址就是会那个错误,而用绝对地址,确能正常运行 ?而换成引用进来的地址确总报以下错误? Traceback (most recent call last): File "C:\EDI_BK\EDI_BK.py", line 81, in OnExec self.bkedi(file_path,target_path) File "C:\EDI_BK\EDI_BK.py", line 94, in bkedi tmpZipFile.write( fileName, fileName, zipfile.ZIP_DEFLATED ) File "D:\Python25\lib\zipfile.py", line 568, in write self.fp.write(zinfo.FileHeader()) File "D:\Python25\lib\zipfile.py", line 260, in FileHeader return header + self.filename + extra UnicodeDecodeError: 'ascii' codec can't decode byte 0xb6 in position 10: ordinal not in range(12 以下是整个程序的原代码,请指点,谢谢 # --*-- encoding: UTF-8 --*-- import wx import compress import os import os,time,stat,zipfile import datetime#日期时间对象 class Frame(wx.Frame): def __init__(self, parent, id): wx.Frame.__init__(self, parent, id, u'EDI文件备份系统' ) panel = wx.Panel(self) self.cfilepath = wx.TextCtrl(panel,-1,'',size=(200,26)) cfileb = wx.Button(panel,label=u'选择备份目录',pos=(200,0)) self.cfilepathd = wx.TextCtrl(panel,-1,'',size=(200,26),pos=(0,30)) cfiled = wx.Button(panel,label=u'备份到',pos=(200,30)) buttonbg=wx.Button(panel,label=u'开始压缩备份',pos=(200,60)) button = wx.Button(panel, label=u"关闭", pos=(300, 10), size=(50, 50)) self.CreateStatusBar() self.SetStatusText(u"EDI文件管理系统 By lvxz 2007-08-08 COSCO" self.Bind(wx.EVT_BUTTON,self.OnChooseF,cfileb) self.Bind(wx.EVT_BUTTON,self.OnBkD,cfiled) self.Bind(wx.EVT_BUTTON,self.OnExec,buttonbg) self.Bind(wx.EVT_BUTTON, self.OnCloseMe, button) self.Bind(wx.EVT_CLOSE, self.OnCloseWindow) def OnCloseMe(self, event): self.Close(True) def OnCloseWindow(self, event): self.Destroy() def OnChooseF(self, evt): # In this case we include a "New directory" button. dlg = wx.DirDialog(self, u"选择目录:", style=wx.DD_DEFAULT_STYLE ) if dlg.ShowModal() == wx.ID_OK: self.cfilepath.SetValue(dlg.GetPath()) else: self.cfilepath.SetValue('') dlg.Destroy() def OnBkD(self, evt): # In this case we include a "New directory" button. dlg = wx.DirDialog(self, u"选择目录:", style=wx.DD_DEFAULT_STYLE ) if dlg.ShowModal() == wx.ID_OK: self.cfilepathd.SetValue(dlg.GetPath()) else: self.cfilepathd.SetValue('') dlg.Destroy() def OnExec(self,evt): file_path = self.cfilepath.GetValue() if os.path.isdir(file_path): pass else: wx.MessageBox(u'当前选择的备份目录不是有效目录,请重新选择','提示') return target_path = self.cfilepathd.GetValue() if os.path.isdir(target_path): pass else: wx.MessageBox(u'目的目录不是有效目录,请重新选择','提示') return #if file_path[-1]!= os.sep:file_path = file_path+ os.sep #if target_path[-1]!= os.sep:target_path = target_path+ os.sep if file_path == target_path: wx.MessageBox(u'不能在同一目录下做备份操作','提示') return #exec_bk = compress.File_Bg(file_path,target_path) self.bkedi(file_path,target_path) def bkedi(self,sours_parth,d_path): target_file = time.strftime('%Y%m%d%H%M%S') + '.zip' #myZipFile = zipfile.ZipFile(target_file, 'w' )# 压缩所有文件,包含子目录 tmpZipFile = zipfile.ZipFile(target_file,'w') source_dir = sours_parth #source_dir = r'C:\MH' #print source_dir for root,dirs,files in os.walk(source_dir): for vfileName in files: fileName = os.path.join(root,vfileName) tmpZipFile.write( fileName, fileName, zipfile.ZIP_DEFLATED ) # 压缩完成 tmpZipFile.close() if __name__ == '__main__': app = wx.PySimpleApp() frame = Frame(parent=None, id=-1) frame.Show() app.MainLoop() |