473,385 Members | 1,359 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Recursive zipping of Directories in Windows

Hi

I am a python novice and I am trying to write a python script (most of
the code is borrowed) to Zip a directory containing some other
directories and files. The script zips all the files fine but when it
tries to zip one of the directories it fails with the following
error:
"IOError: [Errno 13] Permission denied: 'c:\\aaa\\temp'"

The script I am using is:

import zipfile, os

def toZip( directory, zipFile ):
"""Sample for storing directory to a ZipFile"""
z = zipfile.ZipFile(
zipFile, 'w', compression=zipfile.ZIP_DEFLATED
)
def walker( zip, directory, files, root=directory ):
for file in files:
file = os.path.join( directory, file )
# yes, the +1 is hacky...
archiveName = file[len(os.path.commonprefix( (root,
file) ))+1:]
zip.write( file, archiveName, zipfile.ZIP_DEFLATED )
print file
os.path.walk( directory, walker, z )
z.close()
return zipFile
if __name__ == "__main__":
toZip( 'c:\\aaa', 'c:\\aaa\\test.zip' )

I have tried to set the permissions on the folder, but when I check
the directory permissions it is set back to "Read Only"

Any suggestions?

Thanks
Johan Balt

Feb 1 '07 #1
5 2865
Jandre wrote:
Hi

I am a python novice and I am trying to write a python script (most of
the code is borrowed) to Zip a directory containing some other
directories and files. The script zips all the files fine but when it
tries to zip one of the directories it fails with the following
error:
"IOError: [Errno 13] Permission denied: 'c:\\aaa\\temp'"

The script I am using is:

import zipfile, os

def toZip( directory, zipFile ):
"""Sample for storing directory to a ZipFile"""
z = zipfile.ZipFile(
zipFile, 'w', compression=zipfile.ZIP_DEFLATED
)
def walker( zip, directory, files, root=directory ):
for file in files:
file = os.path.join( directory, file )
# yes, the +1 is hacky...
archiveName = file[len(os.path.commonprefix( (root,
file) ))+1:]
zip.write( file, archiveName, zipfile.ZIP_DEFLATED )
print file
os.path.walk( directory, walker, z )
z.close()
return zipFile
if __name__ == "__main__":
toZip( 'c:\\aaa', 'c:\\aaa\\test.zip' )

I have tried to set the permissions on the folder, but when I check
the directory permissions it is set back to "Read Only"

Any suggestions?

Thanks
Johan Balt
Couple of quick suggestions that may help:

1) don't use 'file' as a variable name. It will mask
the builtin file function. If it hasn't bitten you before
it will if you keep doing that.

2) If you put the target .zip file in the directory you are
backing what do you expect the program to do when it comes
to the file you are creating as you walk the directory? You
haven't done anything to 'skip' it.

3) Your commonprefix and +1 appears to result in same
information that the easier to use os.path.basename()
would give you. Double check me on that.

I don't see anything that references C:\\aaa\temp in your
code. Does it exist on your hard drive? If so does it
maybe contain temp files that are open? zipfile module
can't handle open files. You must use try/except to
catch these errors.

Hope info helps.

-Larry
Feb 1 '07 #2
On Feb 1, 9:39 pm, Larry Bates <larry.ba...@websafe.comwrote:
Jandre wrote:
Hi
I am a python novice and I am trying to write a python script (most of
the code is borrowed) to Zip a directory containing some other
directories and files. The script zips all the files fine but when it
tries to zip one of the directories it fails with the following
error:
"IOError: [Errno 13] Permission denied: 'c:\\aaa\\temp'"
The script I am using is:
import zipfile, os
def toZip( directory, zipFile ):
"""Sample for storing directory to a ZipFile"""
z = zipfile.ZipFile(
zipFile, 'w', compression=zipfile.ZIP_DEFLATED
)
def walker( zip, directory, files, root=directory ):
for file in files:
file = os.path.join( directory, file )
# yes, the +1 is hacky...
archiveName = file[len(os.path.commonprefix( (root,
file) ))+1:]
zip.write( file, archiveName, zipfile.ZIP_DEFLATED )
print file
os.path.walk( directory, walker, z )
z.close()
return zipFile
if __name__ == "__main__":
toZip( 'c:\\aaa', 'c:\\aaa\\test.zip' )
I have tried to set the permissions on the folder, but when I check
the directory permissions it is set back to "Read Only"
Any suggestions?
Thanks
Johan Balt

Couple of quick suggestions that may help:

1) don't use 'file' as a variable name. It will mask
the builtin file function. If it hasn't bitten you before
it will if you keep doing that.

2) If you put the target .zip file in the directory you are
backing what do you expect the program to do when it comes
to the file you are creating as you walk the directory? You
haven't done anything to 'skip' it.

3) Your commonprefix and +1 appears to result in same
information that the easier to use os.path.basename()
would give you. Double check me on that.

I don't see anything that references C:\\aaa\temp in your
code. Does it exist on your hard drive? If so does it
maybe contain temp files that are open? zipfile module
can't handle open files. You must use try/except to
catch these errors.

Hope info helps.

-Larry
Thank you Larry.
I've changed the code as epr your advice. The code is now:

import zipfile, os

def toZip( directory, zipFile ):
"""Sample for storing directory to a ZipFile"""
z = zipfile.ZipFile(
zipFile, 'w', compression=zipfile.ZIP_DEFLATED
)
def walker( zip, directory, files, root=directory ):
for f in files:
f = os.path.join( directory, f )
archiveName = os.path.basename(f)
zip.write( f, archiveName, zipfile.ZIP_DEFLATED )
print f
os.path.walk( directory, walker, z )
z.close()
return zipFile
if __name__ == "__main__":
toZip( 'c:\\aaa\\', 'c:\\bbb\\test.zip' )

I still get the same error:
Traceback (most recent call last):
File "C:\Python24\Lib\site-packages\pythonwin\pywin\framework
\scriptutils.py", line 310, in RunScript
exec codeObject in __main__.__dict__
File "C:\Python24\Scripts\dirZip.py", line 20, in ?
toZip( 'c:\\aaa\\', 'c:\\bbb\\test.zip' )
File "C:\Python24\Scripts\dirZip.py", line 14, in toZip
os.path.walk( directory, walker, z )
File "C:\Python24\lib\ntpath.py", line 329, in walk
func(arg, top, names)
File "C:\Python24\Scripts\dirZip.py", line 12, in walker
zip.write( f, archiveName, zipfile.ZIP_DEFLATED )
File "C:\Python24\lib\zipfile.py", line 405, in write
fp = open(filename, "rb")
IOError: [Errno 13] Permission denied: 'c:\\aaa\\temp'

c:\\aaa\\temp is a directory in the directory I an trying to zip. I
want to use this script to back up my work once a day and would like
to
keep the directory structure as is. I can zip the files in c:\aaa\tem
fine so I guess that there aren't any open files in the directory.
Any more ideas?

Feb 4 '07 #3
On Feb 4, 12:42 pm, "Jandre" <jandre_b...@yahoo.co.ukwrote:
On Feb 1, 9:39 pm, Larry Bates <larry.ba...@websafe.comwrote:
Jandre wrote:
Hi
I am a python novice and I am trying to write a python script (most of
the code is borrowed) to Zip a directory containing some other
directories and files. The script zips all the files fine but when it
tries to zip one of the directories it fails with the following
error:
"IOError: [Errno 13] Permission denied: 'c:\\aaa\\temp'"
The script I am using is:
import zipfile, os
def toZip( directory, zipFile ):
"""Sample for storing directory to a ZipFile"""
z = zipfile.ZipFile(
zipFile, 'w', compression=zipfile.ZIP_DEFLATED
)
def walker( zip, directory, files, root=directory ):
for file in files:
file = os.path.join( directory, file )
# yes, the +1 is hacky...
archiveName = file[len(os.path.commonprefix( (root,
file) ))+1:]
zip.write( file, archiveName, zipfile.ZIP_DEFLATED )
print file
os.path.walk( directory, walker, z )
z.close()
return zipFile
if __name__ == "__main__":
toZip( 'c:\\aaa', 'c:\\aaa\\test.zip' )
I have tried to set the permissions on the folder, but when I check
the directory permissions it is set back to "Read Only"
Any suggestions?
Thanks
Johan Balt
Couple of quick suggestions that may help:
1) don't use 'file' as a variable name. It will mask
the builtin file function. If it hasn't bitten you before
it will if you keep doing that.
2) If you put the target .zip file in the directory you are
backing what do you expect the program to do when it comes
to the file you are creating as you walk the directory? You
haven't done anything to 'skip' it.
3) Your commonprefix and +1 appears to result in same
information that the easier to use os.path.basename()
would give you. Double check me on that.
I don't see anything that references C:\\aaa\temp in your
code. Does it exist on your hard drive? If so does it
maybe contain temp files that are open? zipfile module
can't handle open files. You must use try/except to
catch these errors.
Hope info helps.
-Larry

Thank you Larry.
I've changed the code as epr your advice. The code is now:

import zipfile, os

def toZip( directory, zipFile ):
"""Sample for storing directory to a ZipFile"""
z = zipfile.ZipFile(
zipFile, 'w', compression=zipfile.ZIP_DEFLATED
)
def walker( zip, directory, files, root=directory ):
for f in files:
f = os.path.join( directory, f )
archiveName = os.path.basename(f)
zip.write( f, archiveName, zipfile.ZIP_DEFLATED )
print f
os.path.walk( directory, walker, z )
z.close()
return zipFile

if __name__ == "__main__":
toZip( 'c:\\aaa\\', 'c:\\bbb\\test.zip' )

I still get the same error:
Traceback (most recent call last):
File "C:\Python24\Lib\site-packages\pythonwin\pywin\framework
\scriptutils.py", line 310, in RunScript
exec codeObject in __main__.__dict__
File "C:\Python24\Scripts\dirZip.py", line 20, in ?
toZip( 'c:\\aaa\\', 'c:\\bbb\\test.zip' )
File "C:\Python24\Scripts\dirZip.py", line 14, in toZip
os.path.walk( directory, walker, z )
File "C:\Python24\lib\ntpath.py", line 329, in walk
func(arg, top, names)
File "C:\Python24\Scripts\dirZip.py", line 12, in walker
zip.write( f, archiveName, zipfile.ZIP_DEFLATED )
File "C:\Python24\lib\zipfile.py", line 405, in write
fp = open(filename, "rb")
IOError: [Errno 13] Permission denied: 'c:\\aaa\\temp'

c:\\aaa\\temp is a directory in the directory I an trying to zip. I
want to use this script to back up my work once a day and would like
to
keep the directory structure as is. I can zip the files in c:\aaa\tem
fine so I guess that there aren't any open files in the directory.
Any more ideas?
Hi Jandre,

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.

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.

-Josh

Feb 6 '07 #4
On Feb 6, 1:48 am, "joshbl...@gmail.com" <joshbl...@gmail.comwrote:
On Feb 4, 12:42 pm, "Jandre" <jandre_b...@yahoo.co.ukwrote:
On Feb 1, 9:39 pm, Larry Bates <larry.ba...@websafe.comwrote:
Jandre wrote:
Hi
I am a python novice and I am trying to write a python script (most of
the code is borrowed) to Zip a directory containing some other
directories and files. The script zips all the files fine but when it
tries to zip one of the directories it fails with the following
error:
"IOError: [Errno 13] Permission denied: 'c:\\aaa\\temp'"
The script I am using is:
import zipfile, os
def toZip( directory, zipFile ):
"""Sample for storing directory to a ZipFile"""
z = zipfile.ZipFile(
zipFile, 'w', compression=zipfile.ZIP_DEFLATED
)
def walker( zip, directory, files, root=directory ):
for file in files:
file = os.path.join( directory, file )
# yes, the +1 is hacky...
archiveName = file[len(os.path.commonprefix( (root,
file) ))+1:]
zip.write( file, archiveName, zipfile.ZIP_DEFLATED )
print file
os.path.walk( directory, walker, z )
z.close()
return zipFile
if __name__ == "__main__":
toZip( 'c:\\aaa', 'c:\\aaa\\test.zip' )
I have tried to set the permissions on the folder, but when I check
the directory permissions it is set back to "Read Only"
Any suggestions?
Thanks
Johan Balt
Couple of quick suggestions that may help:
1) don't use 'file' as a variable name. It will mask
the builtin file function. If it hasn't bitten you before
it will if you keep doing that.
2) If you put the target .zip file in the directory you are
backing what do you expect the program to do when it comes
to the file you are creating as you walk the directory? You
haven't done anything to 'skip' it.
3) Your commonprefix and +1 appears to result in same
information that the easier to use os.path.basename()
would give you. Double check me on that.
I don't see anything that references C:\\aaa\temp in your
code. Does it exist on your hard drive? If so does it
maybe contain temp files that are open? zipfile module
can't handle open files. You must use try/except to
catch these errors.
Hope info helps.
-Larry
Thank you Larry.
I've changed the code as epr your advice. The code is now:
import zipfile, os
def toZip( directory, zipFile ):
"""Sample for storing directory to a ZipFile"""
z = zipfile.ZipFile(
zipFile, 'w', compression=zipfile.ZIP_DEFLATED
)
def walker( zip, directory, files, root=directory ):
for f in files:
f = os.path.join( directory, f )
archiveName = os.path.basename(f)
zip.write( f, archiveName, zipfile.ZIP_DEFLATED )
print f
os.path.walk( directory, walker, z )
z.close()
return zipFile
if __name__ == "__main__":
toZip( 'c:\\aaa\\', 'c:\\bbb\\test.zip' )
I still get the same error:
Traceback (most recent call last):
File "C:\Python24\Lib\site-packages\pythonwin\pywin\framework
\scriptutils.py", line 310, in RunScript
exec codeObject in __main__.__dict__
File "C:\Python24\Scripts\dirZip.py", line 20, in ?
toZip( 'c:\\aaa\\', 'c:\\bbb\\test.zip' )
File "C:\Python24\Scripts\dirZip.py", line 14, in toZip
os.path.walk( directory, walker, z )
File "C:\Python24\lib\ntpath.py", line 329, in walk
func(arg, top, names)
File "C:\Python24\Scripts\dirZip.py", line 12, in walker
zip.write( f, archiveName, zipfile.ZIP_DEFLATED )
File "C:\Python24\lib\zipfile.py", line 405, in write
fp = open(filename, "rb")
IOError: [Errno 13] Permission denied: 'c:\\aaa\\temp'
c:\\aaa\\temp is a directory in the directory I an trying to zip. I
want to use this script to back up my work once a day and would like
to
keep the directory structure as is. I can zip the files in c:\aaa\tem
fine so I guess that there aren't any open files in the directory.
Any more ideas?

Hi Jandre,

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.

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.
In my experience, zip files don't contain nested folders, instead they
contain a 'flat' list of files. Files in subfolders are represented by
relative path names containing slashes and empty subfolders are
represented by relative paths ending in a slash,.for example
"File1.txt", "Folder1/File2.dat", "Folder2/Folder3/". This seems to
work for me! :-)
Feb 6 '07 #5
Jim
On Feb 6, 2:47 pm, "MRAB" <goo...@mrabarnett.plus.comwrote:
On Feb 6, 1:48 am, "joshbl...@gmail.com" <joshbl...@gmail.comwrote:
On Feb 4, 12:42 pm, "Jandre" <jandre_b...@yahoo.co.ukwrote:
On Feb 1, 9:39 pm, Larry Bates <larry.ba...@websafe.comwrote:
Jandre wrote:
Hi
I am a python novice and I am trying to write a python script (most of
the code is borrowed) to Zip a directory containing some other
directories and files. The script zips all the files fine but when it
tries to zip one of the directories it fails with the following
error:
"IOError: [Errno 13] Permission denied: 'c:\\aaa\\temp'"
The script I am using is:
import zipfile, os
def toZip( directory, zipFile ):
"""Sample for storing directory to a ZipFile"""
z = zipfile.ZipFile(
zipFile, 'w', compression=zipfile.ZIP_DEFLATED
)
def walker( zip, directory, files, root=directory ):
for file in files:
file = os.path.join( directory, file )
# yes, the +1 is hacky...
archiveName = file[len(os.path.commonprefix( (root,
file) ))+1:]
zip.write( file, archiveName, zipfile.ZIP_DEFLATED )
print file
os.path.walk( directory, walker, z )
z.close()
return zipFile
if __name__ == "__main__":
toZip( 'c:\\aaa', 'c:\\aaa\\test.zip' )
I have tried to set the permissions on the folder, but when I check
the directory permissions it is set back to "Read Only"
Any suggestions?
Thanks
Johan Balt
Couple of quick suggestions that may help:
1) don't use 'file' as a variable name. It will mask
the builtin file function. If it hasn't bitten you before
it will if you keep doing that.
2) If you put the target .zip file in the directory you are
backing what do you expect the program to do when it comes
to the file you are creating as you walk the directory? You
haven't done anything to 'skip' it.
3) Your commonprefix and +1 appears to result in same
information that the easier to use os.path.basename()
would give you. Double check me on that.
I don't see anything that references C:\\aaa\temp in your
code. Does it exist on your hard drive? If so does it
maybe contain temp files that are open? zipfile module
can't handle open files. You must use try/except to
catch these errors.
Hope info helps.
-Larry
Thank you Larry.
I've changed the code as epr your advice. The code is now:
import zipfile, os
def toZip( directory, zipFile ):
"""Sample for storing directory to a ZipFile"""
z = zipfile.ZipFile(
zipFile, 'w', compression=zipfile.ZIP_DEFLATED
)
def walker( zip, directory, files, root=directory ):
for f in files:
f = os.path.join( directory, f )
archiveName = os.path.basename(f)
zip.write( f, archiveName, zipfile.ZIP_DEFLATED )
print f
os.path.walk( directory, walker, z )
z.close()
return zipFile
if __name__ == "__main__":
toZip( 'c:\\aaa\\', 'c:\\bbb\\test.zip' )
I still get the same error:
Traceback (most recent call last):
File "C:\Python24\Lib\site-packages\pythonwin\pywin\framework
\scriptutils.py", line 310, in RunScript
exec codeObject in __main__.__dict__
File "C:\Python24\Scripts\dirZip.py", line 20, in ?
toZip( 'c:\\aaa\\', 'c:\\bbb\\test.zip' )
File "C:\Python24\Scripts\dirZip.py", line 14, in toZip
os.path.walk( directory, walker, z )
File "C:\Python24\lib\ntpath.py", line 329, in walk
func(arg, top, names)
File "C:\Python24\Scripts\dirZip.py", line 12, in walker
zip.write( f, archiveName, zipfile.ZIP_DEFLATED )
File "C:\Python24\lib\zipfile.py", line 405, in write
fp = open(filename, "rb")
IOError: [Errno 13] Permission denied: 'c:\\aaa\\temp'
c:\\aaa\\temp is a directory in the directory I an trying to zip. I
want to use this script to back up my work once a day and would like
to
keep the directory structure as is. I can zip the files in c:\aaa\tem
fine so I guess that there aren't any open files in the directory.
Any more ideas?
Hi Jandre,
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.
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.

In my experience, zip files don't contain nested folders, instead they
contain a 'flat' list of files. Files in subfolders are represented by
relative path names containing slashes and empty subfolders are
represented by relative paths ending in a slash,.for example
"File1.txt", "Folder1/File2.dat", "Folder2/Folder3/". This seems to
work for me! :-)- Hide quoted text -

- Show quoted text -
What's the matter with PKZIP? It's been around forever.

Feb 6 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: gusmeister | last post by:
Is there a Perl mod that has a recursive file copy function (similar to `cp -r` in Unix or `xcopy` in Windows)? File::Path does have a recursive file deletion function (rmtree) but no recursive...
2
by: Arthur | last post by:
Does anyone know if the new Windows 2003 built-in .zip compressor/decompressor is available to scripting? If so, can you kindly point me in the right direction? I've searched and searched MSDN...
3
by: alanwo | last post by:
For recursive search for files, like http://support.microsoft.com/default.aspx?scid=KB;EN-US;306666, it may lead to "out of stack" error if searching too many files, say millions of files. Do you...
10
by: ibic | last post by:
Just curious: is it possible to recursively list all the directorys/files inside a given directory using standard c i/o library routines only, which can be re-compiled and run on any os supportes c...
4
by: Elmo Watson | last post by:
Is there a way, with the System.IO class, to do a recursive list of a directory structure? For instance, in DirectoryInfo, you have GetDirectories and GetFiles .... In Directory, you have...
0
by: Benjamin Bittner | last post by:
hallo ng, first of all, for the zipping progress i use the ziplib from http://www.icsharpcode.net/OpenSource/SharpZipLib/Default.aspx now to my problem. im trying to build a little application...
9
by: Paul | last post by:
I'm trying to make get my app to delete all the files in a specified folder and all the files within the folders of the specified folder. e.g. Folder 1 contains three files (File1, File2, File3)...
6
by: reidarT | last post by:
I will develop a backup routine with local files (zipped) to an internet server via ftp. How do I zip files in Vb.net? reidarT
3
by: Gabe Matteson | last post by:
I am trying to set the maximum value of the progress bar so that when a user searches through the specified directory they can see their status. the progress bar name is on form2 and is named...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.