Hi Jandre, <br><br>Your code is treating the directory as a file and trying to open it and read its bytes to zip them. You'll need to differentiate between files and directories. <br><br>You'll need to check out the Zip module to see how it expects files that should be nested within folders. I believe you'll need to set the archive name for the nested files to something like \\temp\\file.ext etc. 
<br><br>-Josh<br><br><br><div><span class="gmail_quote">On 4 Feb 2007 11:42:23 -0800, <b class="gmail_sendername">Jandre</b> <<a href="mailto:jandre_balt@yahoo.co.uk">jandre_balt@yahoo.co.uk</a>> wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
On Feb 1, 9:39 pm, Larry Bates <<a href="mailto:larry.ba...@websafe.com">larry.ba...@websafe.com</a>> wrote:<br>> Jandre wrote:<br>> > Hi<br>><br>> > I am a python novice and I am trying to write a python script (most of
<br>> > the code is borrowed) to Zip a directory containing some other<br>> > directories and files. The script zips all the files fine but when it<br>> > tries to zip one of the directories it fails with the following
<br>> > error:<br>> > "IOError: [Errno 13] Permission denied: 'c:\\aaa\\temp'"<br>><br>> > The script I am using is:<br>><br>> > import zipfile, os<br>><br>> > def toZip( directory, zipFile ):
<br>> >     """Sample for storing directory to a ZipFile"""<br>> >     z = zipfile.ZipFile(<br>> >         zipFile, 'w', compression=zipfile.ZIP_DEFLATED<br>> >     )
<br>> >     def walker( zip, directory, files, root=directory ):<br>> >         for file in files:<br>> >             file = os.path.join( directory, file )<br>> >             # yes, the +1 is hacky...
<br>> >             archiveName = file[len(os.path.commonprefix( (root,<br>> > file) ))+1:]<br>> >             zip.write( file, archiveName, zipfile.ZIP_DEFLATED )<br>> >             print file<br>
> >     os.path.walk( directory, walker, z  )<br>> >     z.close()<br>> >     return zipFile<br>><br>> > if __name__ == "__main__":<br>> >     toZip( 'c:\\aaa', 'c:\\aaa\\test.zip' )
<br>><br>> > I have tried to set the permissions on the folder, but when I check<br>> > the directory permissions it is set back to "Read Only"<br>><br>> > Any suggestions?<br>><br>> > Thanks
<br>> > Johan Balt<br>><br>> Couple of quick suggestions that may help:<br>><br>> 1) don't use 'file' as a variable name. It will mask<br>> the builtin file function.  If it hasn't bitten you before
<br>> it will if you keep doing that.<br>><br>> 2) If you put the target .zip file in the directory you are<br>> backing what do you expect the program to do when it comes<br>> to the file you are creating as you walk the directory?  You
<br>> haven't done anything to 'skip' it.<br>><br>> 3) Your commonprefix and +1 appears to result in same<br>> information that the easier to use os.path.basename()<br>> would give you.  Double check me on that.
<br>><br>> I don't see anything that references C:\\aaa\temp in your<br>> code.  Does it exist on your hard drive?  If so does it<br>> maybe contain temp files that are open?  zipfile module<br>> can't handle open files.  You must use try/except to
<br>> catch these errors.<br>><br>> Hope info helps.<br>><br>> -Larry<br><br>Thank you Larry.<br>I've changed the code as epr your advice. The code is now:<br><br>import zipfile, os<br><br>def toZip( directory, zipFile ):
<br>    """Sample for storing directory to a ZipFile"""<br>    z = zipfile.ZipFile(<br>        zipFile, 'w', compression=zipfile.ZIP_DEFLATED<br>    )<br>    def walker( zip, directory, files, root=directory ):
<br>        for f in files:<br>            f = os.path.join( directory, f )<br>            archiveName = os.path.basename(f)<br>            zip.write( f, archiveName, zipfile.ZIP_DEFLATED )<br>            print f<br>    os.path.walk
( directory, walker, z  )<br>    z.close()<br>    return zipFile<br><br><br>if __name__ == "__main__":<br>    toZip( 'c:\\aaa\\', 'c:\\bbb\\test.zip' )<br><br>I still get the same error:<br>Traceback (most recent call last):
<br>  File "C:\Python24\Lib\site-packages\pythonwin\pywin\framework<br>\scriptutils.py", line 310, in RunScript<br>    exec codeObject in __main__.__dict__<br>  File "C:\Python24\Scripts\dirZip.py", line 20, in ?
<br>    toZip( 'c:\\aaa\\', 'c:\\bbb\\test.zip' )<br>  File "C:\Python24\Scripts\dirZip.py", line 14, in toZip<br>    os.path.walk( directory, walker, z  )<br>  File "C:\Python24\lib\ntpath.py", line 329, in walk
<br>    func(arg, top, names)<br>  File "C:\Python24\Scripts\dirZip.py", line 12, in walker<br>    zip.write( f, archiveName, zipfile.ZIP_DEFLATED )<br>  File "C:\Python24\lib\zipfile.py", line 405, in write
<br>    fp = open(filename, "rb")<br>IOError: [Errno 13] Permission denied: 'c:\\aaa\\temp'<br><br>c:\\aaa\\temp is a directory in the directory I an trying to zip. I<br>want to use this script to back up my work once a day and would like
<br>to<br>keep the directory structure as is. I can zip the files in c:\aaa\tem<br>fine so I guess that there aren't any open files in the directory.<br>Any more ideas?<br><br>--<br><a href="http://mail.python.org/mailman/listinfo/python-list">
http://mail.python.org/mailman/listinfo/python-list</a><br></blockquote></div><br>