simple python script to zip files

Tim Chase python.list at tim.thechases.com
Sat Feb 16 13:18:16 EST 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






More information about the Python-list mailing list