zipfile module doesn't allow append

Discussion in 'Python' started by Ritesh Raj Sarraf, Jun 29, 2006.

  1. 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
     
    Ritesh Raj Sarraf, Jun 29, 2006
    #1
    1. Advertising

  2. Ritesh Raj Sarraf

    Roger Miller Guest

    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.)
     
    Roger Miller, Jun 29, 2006
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Geoffrey  Gallaway
    Replies:
    1
    Views:
    1,092
    Chris Uppal
    Apr 10, 2005
  2. Jeff
    Replies:
    2
    Views:
    958
    clintonG
    Sep 19, 2006
  3. HYRY
    Replies:
    10
    Views:
    608
    Bruno Desthuilliers
    Sep 26, 2007
  4. Stef Mientki
    Replies:
    7
    Views:
    685
    R. David Murray
    Jun 12, 2009
  5. Ryan Taylor
    Replies:
    1
    Views:
    692
    Ryan Taylor
    Sep 9, 2004
Loading...

Share This Page