[Tutor] Formatting zip module arguments correctly
Mark Tolonen
metolone+gmane at gmail.com
Mon Apr 6 07:08:17 CEST 2009
The error indicates your source file cannot be read. Did you have it open in an editor that locks it for exclusive use when you ran your program?
Also, the command:
zipfile.ZipFile(target, 'w').write(source)
writes backup_list to the target zipfile, and returns None, assigning the return value to zip_command and passing that to os.system makes no sense. The command above will throw an exception if anything goes wrong (as you found), so the following code is probably what you want:
try:
zipfile.ZipFile(target, 'w').write(source)
except IOError:
print('Backup Failed')
else:
print('Successful backup to',target)
-Mark
"Benjamin Serrato" <benjamin.serrato at gmail.com> wrote in message news:dde7cc5d0904051354v1c103fb2wf28f1827349706c9 at mail.gmail.com...
Please tell me why this code fails. I am new and I don't understand why my formatting of my zip arguments is incorrect. Since I am unsure how to communicate best so I will show the code, the error message, and what I believe is happening.
#!c:\python30# Filename: backup_ver5.pyimport osimport time
import zipfilesource = r'C:\Documents and Settings\Benjamin Serrato\My Documents\python\backup_list'target_dir = r'C:\Documents and Settings\Benjamin Serrato\My Documents\python\backup_dir'
today = target_dir + os.sep + time.strftime('%Y%m%d') now = time.strftime('%H%M%S')comment = input('Enter a comment --> ')if len(comment) == 0: target = today + os.sep + now + '.zip'
else: target = today + os.sep + now + '_' + \ comment.replace(' ', '_') + '.zip'if not os.path.exists(today): os.mkdir(today) print('Successfully created directory', today)
print(target)print(source)zip_command = zipfile.ZipFile(target, 'w').write(source)if os.system(zip_command) == 0: print('Successful backup to', target)else: print('Backup FAILED')
I receive this error message:
Enter a comment -->
C:\Documents and Settings\Benjamin Serrato\My Documents\python\backup_dir\200904
05\154956.zip
C:\Documents and Settings\Benjamin Serrato\My Documents\python\backup_list
Traceback (most recent call last):
File "C:\Documents and Settings\Benjamin Serrato\My Documents\python\backup_ve
r5.py", line 32, in <module>
zip_command = zipfile.ZipFile(target, 'w').write(source)
File "c:\python30\lib\zipfile.py", line 1031, in write
fp = io.open(filename, "rb")
File "C:\Python30\lib\io.py", line 222, in open
closefd)
File "C:\Python30\lib\io.py", line 615, in __init__
_fileio._FileIO.__init__(self, name, mode, closefd)
IOError: [Errno 13] Permission denied: 'C:\\Documents and Settings\\Benjamin Ser
rato\\My Documents\\python\\backup_list'
The two print tests before zip_command is assigned tell me that the two strings are being passed to zipfile.ZipFile() correctly. The traceback tells me I am not calling zipfile.ZipFile() correctly. The error in __init__ makes me more sure of this. Last, the problem seems to be that I am causing my path string to have double backslashes. I can't follow why the IOError shows that.
I used this site to figure out how to use zipfile. zipfile is a class, I import it at the start of the program then I use it and its primary method. I pass the file I would like to write to zipfile.ZipFile('file to write', 'mode') and set the program to open an object set to be writable. Then the command writes the file to the destination folder with a sub-method like so, "".zipfile('files to write').
Where am I going wrong?
------------------------------------------------------------------------------
_______________________________________________
Tutor maillist - Tutor at python.org
http://mail.python.org/mailman/listinfo/tutor
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090405/8da92fa5/attachment-0001.htm>
More information about the Tutor
mailing list