simple python script to zip files

T

T_T

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_T

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

Tim Chase

I'm just starting to learn some Python basics and are not familiar with
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.
.... 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
 
T

T_T

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

Tim Chase

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
 
J

John Machin

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top