文件夹打包其实很方便,我们可以用tar,rar,7z等等,
有时,我们为了打成多个包。如果我们有10个包,不小心掉了一个,那么对方根本就无法解压出来任何信息。
我的程序把文件夹中的多个文件打包成一个独立的压缩文件,这样,即使对方10个文件只拿到5个,那么他也可以解压出文件使用。
本程序特别适合图片打包,尤其是通过网络发给对方,或者是上传到网上的资源。
注:本程序只是调用压缩程序,在使用前最好安装好压缩程序。
#!/usr/bin/python
#coding=utf-8
#makepaceage.py
import getopt
import sys
import glob
import os
def helpmessage():
"""print help message
"""
print "Make package ."
print "Usage1: makepaceage abc.rar 3m /"*.jpg/""
print "1.Filename don't in chinese"
print "2.Format: rar,tgz,bz2,7z "
print ""
sys.exit()
def packit(packagename,serial,fileslist):
"""make a package
@packagename eg. aaa2.rar
@serial the serial of the package eg. 2
@fileslist files list in the package
"""
pos=packagename.rindex(".")
sztempfilename=packagename[:pos] # filename without ext
sztemptype=packagename[pos+1:] # ext
tempname=sztempfilename+str(serial)+"."+sztemptype
#print tempname
makepackage(tempname,fileslist)
def makepackage(packagename,fileslist):
"""make a package
@packagename eg. aaa2.rar
@fileslist files list in the package
"""
pos=packagename.rindex(".")
sztype=packagename[pos+1:]
szcommand=""
flag=1
filesliststring=" ".join(fileslist)
#print packagename
if sztype=="rar" :
szcommand="rar a "+packagename+" "+filesliststring
elif sztype=="gz" or sztype=="tgz":
szcommand= "tar zcvf "+packagename+" "+filesliststring
elif sztype=="7z" :
szcommand= "7z a "+packagename+" "+filesliststring
elif sztype=="bz2" :
szcommand= "tar jcvf "+packagename+" "+filesliststring
else :
szcommand= "other not supported."
flag=0
print szcommand
if flag!=0:
os.system(szcommand)
pass
def getintsize(szsize):
"""get the int size
@szsize, eg. 3m ,3m=3*1024*1024
"""
size1=szsize.strip()
size1=size1.lower()
size1length=len(size1)
size2=size1[:(size1length-1)]
unit=size1[(size1length-1):]
if unit=="b" :
size=size2
elif unit=="k" :
size=int(size2)*1024
elif unit=="m" :
size=int(size2)*1024*1024
else :
size=0
return size
def getHumansize(size):
"""get human reading
@size eg. 1536=1.5k
"""
count=0
value=size
value1=int(value)/1024
while (value1)!=0:
value=value1
count+=1
value1=int(value)/1024
if count==0:
szsize=str(value)+"B"
elif count==1:
szsize=str(value)+"K"
elif count==2:
szsize=str(value)+"M"
elif count==3:
szsize=str(value)+"G"
elif count==4:
szsize=str(value)+"T"
return szsize
if __name__ == '__main__':
optlist, args = getopt.getopt(sys.argv[1:],'',[''])
#print args
argvlen=len(args) #参数长度
if argvlen==0 or argvlen>3:
helpmessage()
packagename=args[0] # eg. studynote.rar
strsize=args[1] # eg. 3m or 600k
fileglob=args[2] # eg. "*.jpg"
size=getintsize(strsize) # defined max size of the compressed package
packagenumber=0 # serial
SumSize=0
fileslist=[]
globfileslist=glob.glob(fileglob)
times=0
for file1 in globfileslist:
#print file1
times+=1
filesize=os.path.getsize(file1) # get the size of the file
SumSize=SumSize+int(filesize)
if SumSize<=size: # not bigger than the max size
fileslist.append(file1)
else:
packagenumber+=1
packit(packagename,packagenumber,fileslist)
#print str(getHumansize(SumSize))
SumSize=filesize
fileslist=[]
fileslist.append(file1)
if len(fileslist)>0: # package the left files
packagenumber+=1
packit(packagename,packagenumber,fileslist)
pass
可到 http://download.csdn.net/source/1303870
下载源文件