[Tutor] tripping up zip

paul at entropia.co.uk paul at entropia.co.uk
Tue Nov 25 13:42:22 EST 2003


Dear list,

I've been trying to make a quick backup script, but its failing to add some files to the 
archive. Any idea where I'm going wrong?
Works fine using nt4/Python 2.2.2 (#37, Oct 14 2002, 17:02:34) [MSC 32 bit (Intel)] on win32



## path module by http://www.jorendorff.com/articles/python/path/
## win xp pro Python 2.3.2 (#49, Oct  2 2003, 20:02:00) [MSC v.1200 32 bit (Intel)] on 
win32



from path import path
import zipfile

f = path('C:\Documents and Settings\XP Pro\My Documents\personal')
nuzip = zipfile.ZipFile('C:\ziptests\ziptest.zip','w')

## this is a document I added to test things and
## it appears in the final archive
## but fails in the for loop and is listed as doing so

x='C:\\Documents and Settings\\XP Pro\\My Documents\\personal\\INDIA TRAVEL 
ITENARY.doc'
nuzip.write(x,x,zipfile.ZIP_DEFLATED)
files = 0
folders = 0
failed =''
failures= 0
bax = f.walkdirs()
for topfiles in f:
    if topfiles.isfile():
#        print topfiles
        files = files + 1
        try:
            nuzip.write(topfiles,topfiles,zipfile.ZIP_DEFLATED)
        except:
            failed=failed+topfiles.name + ' \n'
            failures = failures + 1
  
## this will sweep the sub directories if I ever get it working
      
##for folder in bax:
###   folders = folders + 1
###   for filer in folder:
##        if filer.isfile():
##            #print filer
##            files = files + 1
##            #try:
##                #nuzip.write(filer,filer,zipfile.ZIP_DEFLATED)
##            #except:
##                #failed=failed+filer.name + ' \n'
##                #failures = failures + 1



nuzip.printdir()
nuzip.close()
print 'there are %d folders and %d files with %d failures' %(folders, files, failures)
print 'The following failed: '
print failed

OUTPUT IN SHELL:
File Name                                             Modified             Size
C:/Documents and Settings/XP Pro/My Documents/personal/INDIA TRAVEL 
ITENARY.doc 2003-10-04 21:47:41        54784
C:/Documents and Settings/XP Pro/My Documents/personal/Backup of REF CHECK 
TEMPLATE.wbk 2003-08-12 14:50:00        58368
C:/Documents and Settings/XP Pro/My Documents/personal/cologne.doc 2003-09-13 
10:10:21        65536
C:/Documents and Settings/XP Pro/My Documents/personal/Email to Jim Harvey.doc 
2003-03-24 06:43:34        19456
C:/Documents and Settings/XP Pro/My Documents/personal/handover arrangements 
draft.doc 2003-03-31 12:08:02        77312
C:/Documents and Settings/XP Pro/My Documents/personal/invite.jpg 2003-03-20 
15:19:06        63175
C:/Documents and Settings/XP Pro/My Documents/personal/leafletdave.doc 2003-03-
31 11:48:06        45056
C:/Documents and Settings/XP Pro/My Documents/personal/NMCK REF CHECK 
TEMPLATE.doc 2003-08-14 08:56:29        63488
C:/Documents and Settings/XP Pro/My Documents/personal/REF CHECK 
TEMPLATE.doc 2003-08-12 14:50:11        58368

there are 0 folders and 21 files with 13 failures
The following failed: 
!ebookers flight 4.url 
gy cv.doc 
GY Resignation .doc 
INDIA TRAVEL ITENARY.doc 
INDIA TRAVEL ITENARY2.doc 
INDIA TRAVEL ITENARY3.doc 
invoice.pdf 
PARIS FRANCE ITERNARY.doc 
PERFORMANCE APPRAISAL FORM.doc 
Reg net password details.doc 
SUe PERFORMANCE APPRAISAL FORM.doc 
Thumbs.db 
which.txt 

without the try clause I got this:

Traceback (most recent call last):
  File "C:\Python23\Lib\mybackup.py", line 22, in -toplevel-
    nuzip.write(filer,filer,zipfile.ZIP_DEFLATED)
  File "C:\Python23\Lib\zipfile.py", line 412, in write
    self.fp.write(zinfo.FileHeader())
  File "C:\Python23\Lib\zipfile.py", line 166, in FileHeader
    return header + self.filename + self.extra
UnicodeDecodeError: 'ascii' codec can't decode byte 0x82 in position 12: ordinal not in 
range(128)



-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20031125/b59d12b3/attachment-0001.html


More information about the Tutor mailing list