There we go with yet another exercise in Python :-)
The idea here is to archive several folders, having each folder in a ZIP file. So if you have two folders, A and B, you end up with A.zip and B.zip.
No doubt this can be done with shell programming (and it will be much better since this solution doesn't provide support for recursion) but this is intended as a way of improving my python skills, so no moaning about the choice of language here! ;-)
And here is the little script:
import os import sys import zipfile if len(sys.argv) > 1: folder = sys.argv else: folder = '.' for item in os.listdir(folder): full_path = os.path.join(folder, item) if not os.path.isdir(full_path): continue dst_filename = item + '.zip' dst_item = os.path.join(folder, dst_filename) if os.path.exists(dst_item) and os.path.getsize(dst_item) > 0: continue output_file = zipfile.ZipFile(dst_item, "w", zipfile.ZIP_DEFLATED) for item_file in os.listdir(full_path): output_file.write(os.path.join(full_path, item_file), item_file) output_file.close()
python zip_folders.py name_of_folder_to_process
Maybe changing the way I do the imports would help, so instead of doing import os and then using os.listdir, I would write from os import listdir and then simply use listdir from then on, but for some reason I kind of prefer the more verbose way for now, so that I don't have to guess where does each function come from.
I suppose once I get more fluent with Python, I won't need to use those long statements.