如何在 python 中压缩文件以及如何指定压缩级别?
到目前为止,我有以下代码,我得到的错误是:
Zip("log4j.dl-service.log.2013-05-03-22",9)
AttributeError: 'str' object has no attribute 'ZipFile'
代码:
import zipfile
import fileinput
def Zip(file,level):
"""
This function uses the zip library native to python to compress files with
Usage: Zip(file,level)
"""
if file and level:
try:
zipfile = file+'.zip'
output = zipfile.ZipFile(zipfile, 'wb')
for line in fileinput.input(file):
output.write(line)
output.close()
if os.path.exists(zipfile):
return zipfile
else:
logMe('critical',"zip failed in Zip()")
return False
except Exception,e:
logMe('critical',e)