python用来压缩目录的脚本

import zipfile,os,sys

def zip_dir(dirname, zipfilename):
    filelist = []
    if os.path.isfile(dirname):
        filelist.append(dirname)
    else:
        for root, dirs, files in os.walk(dirname):
            for name in files:
                filelist.append(os.path.join(root, name))
    f = zipfile.ZipFile(zipfilename, "w", zipfile.ZIP_DEFLATED)  #ZIP_STORE, zipfile.ZIP_DEFLATED
    for tar in filelist:
        arcname = tar[len(dirname):]
        f.write(tar, arcname)
    f.close()

if len(sys.argv) > 2 and sys.argv[1] != "" and sys.argv[2] != "":
    zip_dir(sys.argv[1], sys.argv[2])
else:
    print("#error params")

使用方法如下:

compress.py [dir] [zipfile]

python compress.py [dir] [zipfile]

 

posted @ 2016-03-10 10:45  yans  阅读(922)  评论(0编辑  收藏  举报