python压缩教程_Python压缩和解压缩zip文件

#/usr/bin/python

#coding=utf-8

import os,sys,time

import zipfile

filename = 'callofdutyblackopszombies_1349649132343_my.zip' #要解压的文件

filedir = 'data/' #解压后放入的目录

r = zipfile.is_zipfile(filename)

if r:

starttime = time.time()

fz = zipfile.ZipFile(filename,'r')

for file in fz.namelist():

print(file) #打印zip归档中目录

fz.extract(file,filedir)

endtime = time.time()

times = endtime - starttime

else:

print('This file is not zip file')

print('times' + str(times))

python压缩文件夹为zip

复制代码 代码如下:

#/usr/bin/python

#coding=utf-8

import os

import zipfile

import sys

try:

import zlib

compression = zipfile.ZIP_DEFLATED

except:

compression = zipfile.ZIP_STORED

path = 'data/' #要进行压缩的文档目录

start = path.rfind(os.sep) + 1

filename = 'callofdutyblackopszombies_1349649132343_my.zip' #压缩后的文件名

z = zipfile.ZipFile(filename,mode = "w",compression = compression)

try:

for dirpath,dirs,files in os.walk(path):

for file in files:

if file == filename or file == "zip.py":

continue

print(file)

z_path = os.path.join(dirpath,file)

z.write(z_path,z_path[start:])

z.close()

except:

if z:

z.close()

本文原创发布php中文网,转载请注明出处,感谢您的尊重!

相关文章

相关视频

网友评论

文明上网理性发言,请遵守 新闻评论服务协议我要评论

立即提交

专题推荐独孤九贱-php全栈开发教程

全栈 100W+

主讲:Peter-Zhu 轻松幽默、简短易学,非常适合PHP学习入门

玉女心经-web前端开发教程

入门 50W+

主讲:灭绝师太 由浅入深、明快简洁,非常适合前端学习入门

天龙八部-实战开发教程

实战 80W+

主讲:西门大官人 思路清晰、严谨规范,适合有一定web编程基础学习

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值