simple python script to zip files

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • T_T

    simple python script to zip files

    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

    #2
    Re: simple python script to zip files

    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.

    Comment

    • Tim Chase

      #3
      Re: simple python script to zip files

      >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.splitex t(fname)
      .... if ext.lower().end swith('zip'): continue
      .... f = zipfile.ZipFile ('%s.zip' % basename, 'w')
      .... f.write(fname)
      .... f.close()
      .... print fname
      ....

      seems to do the trick for me.

      -tkc



      Comment

      • T_T

        #4
        Re: simple python script to zip files

        >
        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.


        Comment

        • Tim Chase

          #5
          Re: simple python script to zip files

          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=zip file.ZIP_DEFLAT ED)

          You can read more at



          -tkc




          Comment

          • T_T

            #6
            Re: simple python script to zip files

            f = zipfile.ZipFile (zipfilename, 'w',
            compression=zip file.ZIP_DEFLAT ED)
            >
            -tkc
            Adding the compression rule works great, thanks again!

            Comment

            Working...