歡迎您光臨本站 註冊首頁

linux下用python來壓縮文件夾

←手機掃碼閱讀     火星人 @ 2014-03-09 , reply:0
今天在一個論壇上看到一篇文章,是關於如何用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!!!" 嘿嘿,代碼看過了,下面看看效果吧:[[email protected] 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


[[email protected] python]# pwd/root/python
[[email protected] 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!!![[email protected] 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[[email protected] python]#

看,是否在當前路徑下面多了壓縮的文件,嘿嘿,有興趣的話,趕緊去試試吧...

本文出自 「你是路人甲還是霍元甲」 博客,請務必保留此出處http://world77.blog.51cto.com/414605/467555


[火星人 ] linux下用python來壓縮文件夾已經有461次圍觀

http://coctec.com/docs/linux/show-post-48033.html