快速业务通道

linux下用python来压缩文件夹

作者 佚名技术 来源 Linux系统 浏览 发布时间 2012-04-01
今天在一个论坛上看到一篇文章,是关于如何用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 osimport zipfileimport timefilelist=[]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]# lsbreak.py function_global.py mymodule_demo.py using_name.pycCompressFolder.py function_key.py mymodule.py using_sys.pycontinue.py function_local.py mymodule.pyc using_tuple.pyDocString.py function_param.py test1.zip var.pyexpression.py function_return.py test3.zip vbar.pyfor.py helloworld.py test.zip while.pyfunction1.py if.py using_list.pyfunction_default.py mymodule_demo2.py using_name.py
Empire CMS,phome.net

[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.zipPlease wait,Compressing file...Compressed file success!!![root@localhost python]# lsbreak.py function_global.py mymodule_demo.py using_name.pyCompressFolder.py function_key.py mymodule.py using_name.pyccontinue.py function_local.py mymodule.pyc using_sys.pyDocString.py function_param.py test1.zip using_tuple.pyexpression.py function_return.py test2.zip var.pyfor.py helloworld.py test3.zip vbar.pyfunction1.py if.py test.zip while.pyfunction_default.py mymodule_demo2.py using_list.py[root@localhost python]#

看,是否在当前路径下面多了压缩的文件,嘿嘿,有兴趣的话,赶紧去试试吧...

本文出自 “你是路人甲还是霍元甲” 博客,请务必保留此出处http://world77.blog.51cto.com/414605/467555

Empire CMS,phome.net

凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

分享到: 更多

Copyright ©1999-2011 厦门凌众科技有限公司 厦门优通互联科技开发有限公司 All rights reserved

地址(ADD):厦门软件园二期望海路63号701E(东南融通旁) 邮编(ZIP):361008

电话:0592-5908028 传真:0592-5908039 咨询信箱:web@lingzhong.cn 咨询OICQ:173723134

《中华人民共和国增值电信业务经营许可证》闽B2-20100024  ICP备案:闽ICP备05037997号