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.<br><br><pre class="prettyprint"><code><span class="com"><font style="font-family: courier new,monospace;" size="2">#!c:\python30<br># Filename: backup_ver5.py<br><br>import os<br>import time<br>
import zipfile<br><br><br>source = r'C:\Documents and Settings\Benjamin Serrato\My Documents\python\backup_list'<br><br>target_dir = r'C:\Documents and Settings\Benjamin Serrato\My Documents\python\backup_dir'<br>
<br>today = target_dir + os.sep + time.strftime('%Y%m%d') <br><br>now = time.strftime('%H%M%S')<br><br>comment = input('Enter a comment --> ')<br><br>if len(comment) == 0:<br>        target = today + os.sep + now + '.zip'<br>
else:<br>        target = today + os.sep + now + '_' + \<br>        comment.replace(' ', '_') + '.zip'<br><br>if not os.path.exists(today):<br>        os.mkdir(today)<br>        print('Successfully created directory', today)<br>
<br><br>print(target)<br>print(source)<br>zip_command = zipfile.ZipFile(target, 'w').write(source)<br><br>if os.system(zip_command) == 0:<br>        print('Successful backup to', target)<br>else:<br>        print('Backup FAILED')</font><br>
</span><span class="pun"></span><span class="pln"><br></span></code></pre>I receive this error message:<br><br><span style="font-family: courier new,monospace;">Enter a comment --></span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">C:\Documents and Settings\Benjamin Serrato\My Documents\python\backup_dir\200904</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">05\154956.zip</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">C:\Documents and Settings\Benjamin Serrato\My Documents\python\backup_list</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">Traceback (most recent call last):</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> File "C:\Documents and Settings\Benjamin Serrato\My Documents\python\backup_ve</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">r5.py", line 32, in <module></span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> zip_command = zipfile.ZipFile(target, 'w').write(source)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> File "c:\python30\lib\zipfile.py", line 1031, in write</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> fp = io.open(filename, "rb")</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> File "C:\Python30\lib\io.py", line 222, in open</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> closefd)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> File "C:\Python30\lib\io.py", line 615, in __init__</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> _fileio._FileIO.__init__(self, name, mode, closefd)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">IOError: [Errno 13] Permission denied: 'C:\\Documents and Settings\\Benjamin Ser</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">rato\\My Documents\\python\\backup_list'</span><br><br><p>The two print tests before <code>zip_command</code> is assigned tell me that the two strings are being passed to <code>zipfile.ZipFile()</code> correctly. The traceback tells me I am not calling <code>zipfile.ZipFile()</code> correctly. The error in <code>__init__</code>
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.</p>
<p>I used <a href="http://docs.python.org/dev/3.0/library/zipfile.html" rel="nofollow">this site</a> to figure out how to use <code>zipfile</code>. <code>zipfile</code>
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 <code>zipfile.ZipFile('file to write', 'mode')</code>
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, <code>"".zipfile('files to write')</code>.</p>
<p>Where am I going wrong?</p><br>