Tim Chase
2/16/2008 6:18:00 PM
>> 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