Empty directories with zipfile

Discussion in 'Python' started by Fuzzyman, Jun 9, 2004.

  1. Fuzzyman

    Fuzzyman Guest

    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 an empty
    directory - you need to give it a filename. I know the zip format will
    support it as winzip will allow it... I looked at the zipfile module
    source - and I'm sure it's not impenetrable but I don't wish to learn
    the intricasies of the zip format just yet :)

    Anyone got any ideas - beyond extending the markup to record *all*
    empty directories created as well...... ??

    Regards,

    Fuzzy

    http://www.voidspace.org.uk/atlantibots/pythonutils.html
     
    Fuzzyman, Jun 9, 2004
    #1
    1. Advertising

  2. Fuzzyman

    Peter Hansen Guest

    Fuzzyman wrote:

    > 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 an empty
    > directory - you need to give it a filename. I know the zip format will
    > support it as winzip will allow it...


    Past discussions available from your friend Google:

    http://groups.google.com/groups?q=comp.lang.python zipfile empty directory
     
    Peter Hansen, Jun 9, 2004
    #2
    1. Advertising

  3. Fuzzyman

    Fuzzyman Guest

    Peter Hansen <> wrote in message news:<>...
    > Fuzzyman wrote:
    >
    > > 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 an empty
    > > directory - you need to give it a filename. I know the zip format will
    > > support it as winzip will allow it...

    >
    > Past discussions available from your friend Google:
    >
    > http://groups.google.com/groups?q=comp.lang.python zipfile empty directory


    Ha... thanks....
    Worked a treat.

    Regards,

    Fuzzy

    http://www.voidspace.org.uk/atlantibots/pythonutils.html
     
    Fuzzyman, Jun 11, 2004
    #3
  4. Fuzzyman

    Peter Hansen Guest

    Fuzzyman wrote:

    > Peter Hansen <> wrote in message news:<>...
    >>http://groups.google.com/groups?q=comp.lang.python zipfile empty directory

    >
    > Ha... thanks....
    > Worked a treat.


    For the record, which solution did you use? I see at least
    two alternatives in the second and third hits to the above
    search (attribute 16, attribute 48, and maybe another option
    involving another argument... didn't read it all in detail).

    -Peter
     
    Peter Hansen, Jun 11, 2004
    #4
  5. Fuzzyman

    Fuzzyman Guest

    Peter Hansen <> wrote in message news:<>...
    > Fuzzyman wrote:
    >
    > > Peter Hansen <> wrote in message news:<>...
    > >>http://groups.google.com/groups?q=comp.lang.python zipfile empty directory

    > >
    > > Ha... thanks....
    > > Worked a treat.

    >
    > For the record, which solution did you use? I see at least
    > two alternatives in the second and third hits to the above
    > search (attribute 16, attribute 48, and maybe another option
    > involving another argument... didn't read it all in detail).
    >
    > -Peter


    The simplest one ! Which works fine.....

    from zipfile import ZipFile, ZipInfo
    zip = ZipFile(zipfilename, 'w')
    zi = ZipInfo(emptydirname+'/')
    zip.writestr(zi,'')
    zip.close()

    Create a ZipInfo instance where the filename ends in '/' and use the
    writstr method.

    Only other pitfall - You have to be careful when extracting - test if
    names in the zip.namelist() end with '/' and create the directory
    rather than trying to write the file......

    Regards,


    Fuzzy

    http://www.voidspace.org.uk/atlantibots/pythonutils.html
     
    Fuzzyman, Jun 11, 2004
    #5
    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. Mark Wondratschek

    Python zipfile: Zip empty directories.

    Mark Wondratschek, Aug 29, 2003, in forum: Python
    Replies:
    2
    Views:
    657
    Mark Wondratschek
    Sep 1, 2003
  2. Jim
    Replies:
    0
    Views:
    366
  3. could ildg
    Replies:
    0
    Views:
    491
    could ildg
    Jul 1, 2005
  4. Jeff Epler
    Replies:
    0
    Views:
    410
    Jeff Epler
    Jul 1, 2005
  5. could ildg
    Replies:
    0
    Views:
    522
    could ildg
    Jul 2, 2005
Loading...

Share This Page