Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > simple python script to zip files

Reply
Thread Tools

simple python script to zip files

 
 
T_T
Guest
Posts: n/a
 
      02-16-2008
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.

 
Reply With Quote
 
 
 
 
T_T
Guest
Posts: n/a
 
      02-16-2008
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.
 
Reply With Quote
 
 
 
 
Tim Chase
Guest
Posts: n/a
 
      02-16-2008
>> 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



 
Reply With Quote
 
T_T
Guest
Posts: n/a
 
      02-16-2008
>
> 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.


 
Reply With Quote
 
Tim Chase
Guest
Posts: n/a
 
      02-16-2008
> 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




 
Reply With Quote
 
T_T
Guest
Posts: n/a
 
      02-16-2008
> f = zipfile.ZipFile(zipfilename, 'w',
> compression=zipfile.ZIP_DEFLATED)
>
> -tkc


Adding the compression rule works great, thanks again!
 
Reply With Quote
 
John Machin
Guest
Posts: n/a
 
      02-16-2008
On Feb 17, 6:52 am, Tim Chase <python.l...@tim.thechases.com> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to compress a big file into many zip files with Archive::Zip? Bo Yang Perl Misc 9 11-20-2006 11:39 AM
Archive::Zip - how to create huge zip files ? MoshiachNow Perl Misc 2 10-04-2006 09:09 PM
Possibility to add a zip-file to a new zip-file with "add to zip" (right-click) ?? erikkie@casema.nl Computer Support 4 06-26-2006 12:18 AM
Simple script to make .png thumbnails from .zip archive... K P S Python 9 06-21-2006 02:12 AM
java.util.zip - problem opening some legitimite me zip files Alex Hunsley Java 1 09-16-2004 02:06 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57