前段时间碰到一个需要把目录下文件夹压缩的项目,但是度娘里没找到,只好自己写脚本了。

  

#coding:utf-8  
import os
filePath = raw_input("请输入路径:")
if filePath == "":
	os._exit() #需要退出

ds = list(os.walk(filePath)) #获得所有文件夹的信息列表
fileExe = os.path.realpath(__file__)
x_ipos = fileExe.rfind('\\')
fileExe = fileExe[0:x_ipos]
fileExe += "\\bin\\7z.exe"
print fileExe
for d in ds: #遍历该列表
    os.chdir(d[0]) #进入本级路径
    print d[0]
    if d[0] != filePath: #需要过滤目录自己
        print ("\'%s\' a -tzip \"%s.zip\" \"%s\"") % (fileExe,d[0],d[0])
        os.system(("%s a -tzip \"%s.zip\" \"%s\"") % (fileExe,d[0],d[0]))	
os.system("pause")			

  公司规定不能用winrar,并且rar格式的在某些客户电脑上没有办法识别,所以使用了开源软件7z来进行压缩,这个段的代码没啥东西,主要是执行7z的 压缩命令 【7z.exe a -tzip 压缩文件路径 要压缩路径  】 来进行压缩。