zipfile module doesn't allow append

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ritesh Raj Sarraf

    #1

    zipfile module doesn't allow append

    Hi,

    I've got a problem here.

    def compress_the_fi le(zip_file_nam e, files_to_compre ss, sSourceDir):
    """
    Condenses all the files into one single file for easy transfer
    """

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

    try:
    os.chdir(sSourc eDir)
    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.writ e("\nAieee! Some error exception in creating
    zip file %s\n" % (zip_file_name) )
    # sys.exit(1)

    filename.write( files_to_compre ss, files_to_compre ss,
    zipfile.ZIP_DEF LATED)
    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

  • Roger Miller

    #2
    Re: zipfile module doesn't allow append

    Ritesh Raj Sarraf wrote:
    [color=blue]
    > 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 ??[/color]

    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(na me) 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.)

    Comment

    Working...