467,083 Members | 1,209 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,083 developers. It's quick & easy.

zipfile module doesn't allow append

Hi,

I've got a problem here.

def compress_the_file(zip_file_name, files_to_compress, sSourceDir):
"""
Condenses all the files into one single file for easy transfer
"""

try:
import zipfile
except ImportError:
sys.stderr.write("Aieeee! module not found.\n")

try:
os.chdir(sSourceDir)
except:
#TODO: Handle this exception
pass

filename = zipfile.ZipFile(zip_file_name, "a")
# try:
# filename = zipfile.ZipFile(zip_file_name, "a")
# except:
# #TODO Handle the exception
# sys.stderr.write("\nAieee! Some error exception in creating
zip file %s\n" % (zip_file_name))
# sys.exit(1)

filename.write(files_to_compress, files_to_compress,
zipfile.ZIP_DEFLATED)
filename.close()

The line
filename = zipfile.ZipFile(zip_file_name, "a")
throws an exception if the given filename is not present already.
Shouldn't it create a file (in case one is not there) since it is
"append" mode ??
Ritesh

Jun 29 '06 #1
  • viewed: 1565
Share:
1 Reply
Ritesh Raj Sarraf wrote:
The line
filename = zipfile.ZipFile(zip_file_name, "a")
throws an exception if the given filename is not present already.
Shouldn't it create a file (in case one is not there) since it is
"append" mode ??


Perhaps it would be nicer that way, but it is working as documented.
Catch the exception and open in 'w' mode.

To anticipate your next possible problem, note that in append mode if
you write a file that already exists in the archive it will not replace
the existing file, but will add another one with the same name. As far
as I can tell, there is no way to read the newer version because
zipfile.read(name) always finds the first version. So if you are trying
to update a zipfile you will probably have to read the old archive and
write a new one, copying the files you want to keep and replacing the
ones you want to update. (At this point you might want to consider
invoking an external zip utility instead.)

Jun 29 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Tung Wai Yip | last post: by
reply views Thread by Евгений Косенко | last post: by
1 post views Thread by Fuzzyman | last post: by
reply views Thread by Ian Cook | last post: by
2 posts views Thread by Renzo | last post: by
11 posts views Thread by Hari Sekhon | last post: by
3 posts views Thread by Tim | last post: by
15 posts views Thread by Martin Maney | last post: by
1 post views Thread by John Machin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.