今天在一个论坛上看到一篇文章,是关于如何用python来做压缩的,我看了下,对于我这个python新手来说,还是很有学习的意义的,我当即拷贝到我的开发环境下,试验了下,果然可以,
http://www.cnginx.com/read.php?tid=133&fpage=3 这个就是连接,试验成功后,想把这个程序移植到linux下,呵呵,原来的是在window下的。。。
嘿嘿,在经过一番折腾后,终于让我弄出来了,嘿嘿,程序代码如下:
#!/usr/bin/python #这是必须的
# write by world77 #这是广告
# Blog: world77.blog.51cto.com #这是广告
# QQ:384343423 #这是广告
import os
import zipfile
import time
filelist=[]
NeedToCompressDir=raw_input("Please input you want to compress's directory:")
#print NeedToCompressDir #提示要求你输入需要压缩的文件夹路径
CompressedFileName=raw_input( "Please enter the Compressed file name:")
#print CompressFileName
##提示要求你输入压缩后保持的文件名
for root,dirs,files in os.walk(NeedToCompressDir):
for name in files:
filelist.append(os.path.join(root,name))
zf=zipfile.ZipFile(CompressedFileName,'w',zipfile.ZIP_DEFLATED)
print "Please wait,Compressing file..." #提示信息
for i in filelist:
# print "Compressing file,please wait..."
zf.write(i)
time.sleep(0.1) #休眠0.1秒
zf.close()
time.sleep (1) #休眠1秒
print "Compressed file success!!!"
嘿嘿,代码看过了,下面看看效果吧:
[root@localhost python]# ls
break.py function_global.py mymodule_demo.py using_name.pyc
CompressFolder.py function_key.py mymodule.py using_sys.py
continue.py function_local.py mymodule.pyc using_tuple.py
DocString.py function_param.py test1.zip var.py
expression.py function_return.py test3.zip vbar.py
for.py helloworld.py test.zip while.py
function1.py if.py using_list.py
function_default.py mymodule_demo2.py using_name.py
[root@localhost python]# pwd
/root/python
[root@localhost python]# python CompressFolder.py
Please input you want to compress's directory:/root/python
Please enter the Compressed file name:test2.zip
Please wait,Compressing file...
Compressed file success!!!
[root@localhost python]# ls
break.py function_global.py mymodule_demo.py using_name.py
CompressFolder.py function_key.py mymodule.py using_name.pyc
continue.py function_local.py mymodule.pyc using_sys.py
DocString.py function_param.py test1.zip using_tuple.py
expression.py function_return.py test2.zip var.py
for.py helloworld.py test3.zip vbar.py
function1.py if.py test.zip while.py
function_default.py mymodule_demo2.py using_list.py
[root@localhost python]#
看,是否在当前路径下面多了压缩的文件,嘿嘿,有兴趣的话,赶紧去试试吧。。。
本文转自你是路人甲还是霍元甲博客51CTO博客,原文链接http://blog.51cto.com/world77/467555如需转载请自行联系原作者
world77