def plaintext2zip(self, file_name, subdirname, plaintext):
file_name=file_name.split('.')[0]
plaintext_file_name = STATIC_PLAINTEXT_ARTICLES_DIR+subdirname+'/'+file_name+'.txt'
zip_file_name = STATIC_PLAINTEXT_ARTICLES_DIR+subdirname+'/'+file_name+'.zip'
if not os.path.exists(STATIC_PLAINTEXT_ARTICLES_DIR+subdirname):
os.makedirs(STATIC_PLAINTEXT_ARTICLES_DIR+subdirname)
with codecs.open(plaintext_file_name, 'w', encoding='utf8') as outfile:
outfile.write(plaintext)
outfile.flush()
outfile.close()
zf = zipfile.ZipFile(zip_file_name, mode='w', compression=zipfile.ZIP_DEFLATED)
try:
zf.write(plaintext_file_name, os.path.basename(plaintext_file_name))
os.remove(plaintext_file_name)
except Exception, e:
print e
logging.error('zip error %s ' % plaintext_file_name)
finally:
zf.close()
评论列表
文章目录