zipfile module doesn't allow append

R

Ritesh Raj Sarraf

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
 
R

Roger Miller

Ritesh said:
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.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top