473,405 Members | 2,334 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,405 software developers and data experts.

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
1 1743
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Tung Wai Yip | last post by:
Can I add empty directory using zipfile? When I try to add a directory it complains that it is not a file. tung
0
by: Åâãåíèé Êîñåíêî | last post by:
Hi! I need to fresh a zip-archive using zipfile. However, it seems it is no function to fresh the archive. When I use the append-mode of ZipFile, the written file is merely appended to the...
1
by: Fuzzyman | last post by:
I have created a set of classes that will profile a file structure and record all changes as a simple markup and single zipfile of all new/modified files. I can't get the zipfile module to archive...
0
by: Ian Cook | last post by:
Hi, I'm new to python so please excuse me if this is a 'dumb' question. I want to use the ZipFile module to UPDATE, rather than append, or create a new zip file. Does anyone know how this can...
2
by: Renzo | last post by:
Hi, I'm working to create a backup function for a software; in particular, from a directory with 12300 files (Jpg, medium size = 250KB / total size = 2.90GB), i have to create a zip file. I've...
11
by: Hari Sekhon | last post by:
I do import zipfile zip=zipfile.ZipFile('d:\somepath\cdimage.zip') zip.namelist() then either of the two: A) file('someimage.iso','w').write(zip.read('someimage.iso'))
3
by: Tim | last post by:
I'm at the end of my limited experience... I'm using py2exe to create an executable. I'm using bundle level 1. When I don't use the zipfile option, the executable and library.zip get created and...
15
by: Martin Maney | last post by:
To quote from zipfile.py (2.4 library): # Search the last END_BLOCK bytes of the file for the record signature. # The comment is appended to the ZIP file and has a 16 bit length. # So the...
1
by: John Machin | last post by:
On Jun 4, 8:06 pm, jwesonga <crazylun...@gmail.comwrote: Nothing is ever as it seems. Let's try to work backwards from the error message ... and we don't need your magnificent script, just the...
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: 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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.