[Tutor] Formatting zip module arguments correctly
kent37 at tds.net
Mon Apr 6 01:19:41 CEST 2009
On Sun, Apr 5, 2009 at 4:54 PM, Benjamin Serrato
<benjamin.serrato at gmail.com> wrote:
> 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.
> # Filename: backup_ver5.py
> import os
> import time
> import zipfile
> source = r'C:\Documents and Settings\Benjamin Serrato\My
> target_dir = r'C:\Documents and Settings\Benjamin Serrato\My
> 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'
> target = today + os.sep + now + '_' + \
> comment.replace(' ', '_') + '.zip'
> if not os.path.exists(today):
> print('Successfully created directory', today)
> zip_command = zipfile.ZipFile(target, 'w').write(source)
The argument to write() must be a file path, are you giving it a
> if os.system(zip_command) == 0:
> print('Successful backup to', target)
> print('Backup FAILED')
> I receive this error message:
> 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
> 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
> rato\\My Documents\\python\\backup_list'
Notice that it is the write() call that is failing, not ZipFile(), and
the error is a read error on target.
> 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.
No, it is FileIO.__init__() that is failing, not ZipFile.__init__()
> 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').
It should be zipfile('file to write')
More information about the Tutor