simple python script to zip files

Discussion in 'Python' started by T_T, Feb 16, 2008.

  1. T_T

    T_T Guest

    Hi,

    I'm just starting to learn some Python basics and are not familiar with
    file handling.
    Looking for a python scrip that zips files.
    So aaa.xx bbb.yy ccc.xx should be zipped to aaa.zip bbb.zip ccc.zip

    I haven't been able to type more than 'import gzip'..

    Just a script I need for practical use (zip didn't do the job) and not
    for educational purposes.
     
    T_T, Feb 16, 2008
    #1
    1. Advertising

  2. T_T

    T_T Guest

    On Sat, 16 Feb 2008 15:37:16 +0000, T_T wrote:

    > Hi,
    >
    > I'm just starting to learn some Python basics and are not familiar with
    > file handling.
    > Looking for a python scrip that zips files. So aaa.xx bbb.yy ccc.xx
    > should be zipped to aaa.zip bbb.zip ccc.zip
    >
    > I haven't been able to type more than 'import gzip'..
    >
    > Just a script I need for practical use (zip didn't do the job) and not
    > for educational purposes.


    btw. I need it for batch handling, so I want to place the file in a
    directory, run it and zip all files in the directory.

    Hope someone will be able to help me.
     
    T_T, Feb 16, 2008
    #2
    1. Advertising

  3. T_T

    Tim Chase Guest

    >> I'm just starting to learn some Python basics and are not familiar with
    >> file handling.
    >> Looking for a python scrip that zips files. So aaa.xx bbb.yy ccc.xx
    >> should be zipped to aaa.zip bbb.zip ccc.zip
    >>
    >> I haven't been able to type more than 'import gzip'..


    Well, you ask for zip files, but then import gzip... ?

    > btw. I need it for batch handling, so I want to place the file in a
    > directory, run it and zip all files in the directory.


    >>> import os, zipfile
    >>> for fname in os.listdir('.'):

    .... basename, ext = os.path.splitext(fname)
    .... if ext.lower().endswith('zip'): continue
    .... f = zipfile.ZipFile('%s.zip' % basename, 'w')
    .... f.write(fname)
    .... f.close()
    .... print fname
    ....

    seems to do the trick for me.

    -tkc
     
    Tim Chase, Feb 16, 2008
    #3
  4. T_T

    T_T Guest

    >
    > seems to do the trick for me.
    >
    > -tkc


    Thanks! Works indeed. Strange thing is though, the files created are the
    exact size as the original file. So it seems like it is zipping without
    compression.
     
    T_T, Feb 16, 2008
    #4
  5. T_T

    Tim Chase Guest

    > Thanks! Works indeed. Strange thing is though, the files created are the
    > exact size as the original file. So it seems like it is zipping without
    > compression.



    The instantiation of the ZipFile object can take an optional
    parameter to control the compression. The zipfile module only
    supports storing (the default as you discovered) and "deflation":

    f = zipfile.ZipFile(zipfilename, 'w',
    compression=zipfile.ZIP_DEFLATED)

    You can read more at

    http://docs.python.org/lib/zipfile-objects.html

    -tkc
     
    Tim Chase, Feb 16, 2008
    #5
  6. T_T

    T_T Guest

    > f = zipfile.ZipFile(zipfilename, 'w',
    > compression=zipfile.ZIP_DEFLATED)
    >
    > -tkc


    Adding the compression rule works great, thanks again!
     
    T_T, Feb 16, 2008
    #6
  7. T_T

    John Machin Guest

    On Feb 17, 6:52 am, Tim Chase <> wrote:
    > > Thanks! Works indeed. Strange thing is though, the files created are the
    > > exact size as the original file. So it seems like it is zipping without
    > > compression.

    >
    > The instantiation of the ZipFile object can take an optional
    > parameter to control the compression. The zipfile module only
    > supports storing (the default as you discovered) and "deflation":
    >
    > f = zipfile.ZipFile(zipfilename, 'w',
    > compression=zipfile.ZIP_DEFLATED)
    >
    > You can read more at
    >
    > http://docs.python.org/lib/zipfile-objects.html


    Confession: An infrequent user of the zipfile module, I've been caught
    twice by this strange choice of default argument.

    I wonder how many people actually need/want the stored (uncompressed )
    format. The select few must surely document that their choice is
    deliberate:

    f = zipfile.ZipFile(
    zipfilename,
    mode='w',
    compression=zipfile.ZIP_STORED, # no compression
    # Yes the above is deliberate.
    # See section n.nn of project specs.
    )

    Suggestion: for the benefit of folk who (unlike the OP and I) do read
    the manual, it might help if the text had one paragraph per arg, and
    included a warning:
    """
    .... also works, and at least WinZip can read such files.

    /compression/ is the ZIP compression method to use when writing the
    archive, and should be ZIP_STORED or ZIP_DEFLATED. *WARNING:* the
    default is ZIP_STORED (no compression). Unrecognized values will cause
    RuntimeError to be raised. If ZIP_DEFLATED is specified but the zlib
    module is not available, RuntimeError is also raised.

    If /allowZip64/ is True ...
    """

    Cheers,
    John
     
    John Machin, Feb 16, 2008
    #7
    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. Alex Hunsley
    Replies:
    1
    Views:
    601
    Andrew Thompson
    Sep 16, 2004
  2. Replies:
    0
    Views:
    1,725
  3. HARI OM
    Replies:
    0
    Views:
    17,421
    HARI OM
    May 3, 2005
  4. MoshiachNow
    Replies:
    2
    Views:
    275
    Ilya Zakharevich
    Oct 4, 2006
  5. Bo Yang
    Replies:
    9
    Views:
    304
    -berlin.de
    Nov 20, 2006
Loading...

Share This Page