首页 > 代码库 > Python challenge 7 - zipfile
Python challenge 7 - zipfile
第七题地址:http://www.pythonchallenge.com/pc/def/channel.html
根据提示,我们看到<!-- <-- zip -->,试一试之后发现可以把channel.html改成channel.zip,然后下载得到一个zip文件。
打开zip文件,其中的readme.txt,我们得到如下的提示
welcome to my zipped list. hint1: start from 90052 hint2: answer is inside the zip
我们找到90052.txt
Next nothing is 94191
看到又是nothing,并且指向了94191,很明显是下一个文件。
import zipfile import re filename = 'C:\Python27\channel.zip' def process(): file_list = zipfile.ZipFile(filename) regex = re.compile(r'(\d+)$') nothing = '90052' try: while True: fn = '%s.txt' % nothing print file_list.read(fn, 'r') nothing = regex.search(file_list.read(fn, 'r')).group() except: pass if __name__ == '__main__': process()
运行完,得到如下结果
Next nothing is 94191 Next nothing is 85503 Next nothing is 70877 Next nothing is 15409 Next nothing is 60838 Next nothing is 80009 Next nothing is 21347 Next nothing is 24164 Next nothing is 13836 Next nothing is 80123 Next nothing is 92453 Next nothing is 69556 Next nothing is 31491 Next nothing is 13371 Next nothing is 88143 Next nothing is 82593 Next nothing is 30645 Next nothing is 88985 Next nothing is 27190 Next nothing is 24448 Next nothing is 37468 Next nothing is 49892 Next nothing is 54714 Next nothing is 75161 Next nothing is 893 Next nothing is 57882 Next nothing is 40033 Next nothing is 29521 Next nothing is 5289 Next nothing is 4701 Next nothing is 32742 Next nothing is 18751 Next nothing is 68586 Next nothing is 53984 Next nothing is 52184 Next nothing is 43807 Next nothing is 56300 Next nothing is 97649 Next nothing is 88244 Next nothing is 91669 Next nothing is 88394 Next nothing is 77575 Next nothing is 70208 Next nothing is 78211 Next nothing is 84854 Next nothing is 87594 Next nothing is 26234 Next nothing is 63181 Next nothing is 68559 Next nothing is 37695 Next nothing is 64397 Next nothing is 55356 Next nothing is 8258 Next nothing is 77074 Next nothing is 28389 Next nothing is 39983 Next nothing is 40210 Next nothing is 69972 Next nothing is 48089 Next nothing is 34764 Next nothing is 46945 Next nothing is 97812 Next nothing is 95662 Next nothing is 55220 Next nothing is 86793 Next nothing is 69785 Next nothing is 85827 Next nothing is 75722 Next nothing is 37259 Next nothing is 57622 Next nothing is 57566 Next nothing is 98181 Next nothing is 57831 Next nothing is 68947 Next nothing is 29834 Next nothing is 67060 Next nothing is 48669 Next nothing is 12761 Next nothing is 47882 Next nothing is 98339 Next nothing is 9058 Next nothing is 14745 Next nothing is 60138 Next nothing is 47842 Next nothing is 71178 Next nothing is 1091 Next nothing is 52205 Next nothing is 98709 Next nothing is 4992 Next nothing is 97673 Next nothing is 92736 Next nothing is 98119 Next nothing is 97515 Next nothing is 98939 Next nothing is 36656 Next nothing is 85839 Next nothing is 33746 Next nothing is 64122 Next nothing is 41968 Next nothing is 56617 Next nothing is 83808 Next nothing is 68613 Next nothing is 99714 Next nothing is 67196 Next nothing is 11992 Next nothing is 38192 Next nothing is 61837 Next nothing is 29034 Next nothing is 77480 Next nothing is 12737 Next nothing is 39310 Next nothing is 89307 Next nothing is 36624 Next nothing is 81412 Next nothing is 51450 Next nothing is 21690 Next nothing is 46726 Next nothing is 74576 Next nothing is 57901 Next nothing is 92043 Next nothing is 78130 Next nothing is 23647 Next nothing is 39258 Next nothing is 76629 Next nothing is 48614 Next nothing is 74736 Next nothing is 53027 Next nothing is 3713 Next nothing is 30339 Next nothing is 42197 Next nothing is 12539 Next nothing is 98095 Next nothing is 176 Next nothing is 64956 Next nothing is 78307 Next nothing is 35035 Next nothing is 36894 Next nothing is 76017 Next nothing is 67036 Next nothing is 33179 Next nothing is 64085 Next nothing is 90506 Next nothing is 94079 Next nothing is 76441 Next nothing is 16146 Next nothing is 78399 Next nothing is 98044 Next nothing is 64045 Next nothing is 69444 Next nothing is 78862 Next nothing is 51492 Next nothing is 52349 Next nothing is 86079 Next nothing is 8001 Next nothing is 68750 Next nothing is 65764 Next nothing is 11419 Next nothing is 96644 Next nothing is 63154 Next nothing is 70397 Next nothing is 91046 Next nothing is 61274 Next nothing is 50678 Next nothing is 94118 Next nothing is 86774 Next nothing is 47673 Next nothing is 90792 Next nothing is 95791 Next nothing is 72442 Next nothing is 57730 Next nothing is 79566 Next nothing is 66349 Next nothing is 40521 Next nothing is 59808 Next nothing is 14628 Next nothing is 39820 Next nothing is 100 Next nothing is 84844 Next nothing is 39890 Next nothing is 12930 Next nothing is 97979 Next nothing is 16870 Next nothing is 3928 Next nothing is 71245 Next nothing is 8289 Next nothing is 9118 Next nothing is 81768 Next nothing is 68242 Next nothing is 65768 Next nothing is 94363 Next nothing is 34013 Next nothing is 85632 Next nothing is 82706 Next nothing is 97089 Next nothing is 67999 Next nothing is 53542 Next nothing is 68609 Next nothing is 91136 Next nothing is 95660 Next nothing is 30395 Next nothing is 19822 Next nothing is 55325 Next nothing is 33630 Next nothing is 65277 Next nothing is 34563 Next nothing is 44890 Next nothing is 54001 Next nothing is 75838 Next nothing is 83931 Next nothing is 68010 Next nothing is 62369 Next nothing is 30318 Next nothing is 79479 Next nothing is 31117 Next nothing is 43267 Next nothing is 9623 Next nothing is 46206 Next nothing is 56408 Next nothing is 91586 Next nothing is 60171 Next nothing is 21279 Next nothing is 74921 Next nothing is 91523 Next nothing is 82519 Next nothing is 40257 Next nothing is 76116 Next nothing is 7556 Next nothing is 99775 Next nothing is 74219 Next nothing is 82223 Next nothing is 35247 Next nothing is 52390 Next nothing is 89160 Next nothing is 16586 Next nothing is 24065 Next nothing is 69983 Next nothing is 55163 Next nothing is 73389 Next nothing is 91965 Next nothing is 98094 Next nothing is 11616 Next nothing is 3651 Next nothing is 56582 Next nothing is 70915 Next nothing is 16041 Next nothing is 22628 Next nothing is 21140 Next nothing is 24595 Next nothing is 2015 Next nothing is 1063 Next nothing is 81810 Next nothing is 16744 Next nothing is 63875 Next nothing is 55449 Next nothing is 93756 Next nothing is 90763 Next nothing is 92265 Next nothing is 12362 Next nothing is 31981 Next nothing is 33177 Next nothing is 64602 Next nothing is 95085 Next nothing is 22509 Next nothing is 16043 Next nothing is 75290 Next nothing is 50374 Next nothing is 67572 Next nothing is 37055 Next nothing is 77186 Next nothing is 32544 Next nothing is 1878 Next nothing is 65126 Next nothing is 72092 Next nothing is 17712 Next nothing is 81039 Next nothing is 87476 Next nothing is 18375 Next nothing is 29494 Next nothing is 46210 Next nothing is 12351 Next nothing is 11352 Next nothing is 16030 Next nothing is 3390 Next nothing is 603 Next nothing is 11827 Next nothing is 1123 Next nothing is 3924 Next nothing is 66402 Next nothing is 36832 Next nothing is 76131 Next nothing is 51106 Next nothing is 24644 Next nothing is 66351 Next nothing is 34515 Next nothing is 3572 Next nothing is 66200 Next nothing is 63804 Next nothing is 47248 Next nothing is 4931 Next nothing is 41894 Next nothing is 88223 Next nothing is 76945 Next nothing is 18520 Next nothing is 1440 Next nothing is 39405 Next nothing is 72571 Next nothing is 4506 Next nothing is 38221 Next nothing is 82852 Next nothing is 62826 Next nothing is 24567 Next nothing is 4682 Next nothing is 88024 Next nothing is 33791 Next nothing is 29446 Next nothing is 67233 Next nothing is 17152 Next nothing is 730 Next nothing is 28928 Next nothing is 88004 Next nothing is 65514 Next nothing is 88301 Next nothing is 74357 Next nothing is 32837 Next nothing is 59049 Next nothing is 91793 Next nothing is 43083 Next nothing is 97338 Next nothing is 22601 Next nothing is 27898 Next nothing is 56149 Next nothing is 95637 Next nothing is 39483 Next nothing is 94070 Next nothing is 92752 Next nothing is 95205 Next nothing is 73840 Next nothing is 38673 Next nothing is 78668 Next nothing is 72782 Next nothing is 50761 Next nothing is 68023 Next nothing is 27107 Next nothing is 61416 Next nothing is 18525 Next nothing is 26202 Next nothing is 34548 Next nothing is 48464 Next nothing is 18987 Next nothing is 2539 Next nothing is 16362 Next nothing is 97400 Next nothing is 35559 Next nothing is 49787 Next nothing is 27738 Next nothing is 58043 Next nothing is 65166 Next nothing is 63997 Next nothing is 20717 Next nothing is 24530 Next nothing is 21174 Next nothing is 68030 Next nothing is 55286 Next nothing is 17904 Next nothing is 86545 Next nothing is 95763 Next nothing is 18922 Next nothing is 27319 Next nothing is 61270 Next nothing is 1064 Next nothing is 8424 Next nothing is 59672 Next nothing is 51066 Next nothing is 44848 Next nothing is 22633 Next nothing is 25923 Next nothing is 58240 Next nothing is 51783 Next nothing is 33131 Next nothing is 75249 Next nothing is 49394 Next nothing is 87771 Next nothing is 404 Next nothing is 93575 Next nothing is 82483 Next nothing is 78096 Next nothing is 28374 Next nothing is 58341 Next nothing is 75198 Next nothing is 64511 Next nothing is 44038 Next nothing is 25136 Next nothing is 88199 Next nothing is 46518 Next nothing is 71787 Next nothing is 68974 Next nothing is 75307 Next nothing is 12176 Next nothing is 62030 Next nothing is 49856 Next nothing is 64466 Next nothing is 668 Next nothing is 57709 Next nothing is 52400 Next nothing is 94833 Next nothing is 65415 Next nothing is 80057 Next nothing is 73422 Next nothing is 86874 Next nothing is 94159 Next nothing is 16466 Next nothing is 6549 Next nothing is 92525 Next nothing is 40627 Next nothing is 27531 Next nothing is 88177 Next nothing is 4465 Next nothing is 99189 Next nothing is 47856 Next nothing is 83694 Next nothing is 54379 Next nothing is 47454 Next nothing is 87096 Next nothing is 3469 Next nothing is 99254 Next nothing is 4630 Next nothing is 75878 Next nothing is 21776 Next nothing is 92781 Next nothing is 23060 Next nothing is 89587 Next nothing is 18346 Next nothing is 47867 Next nothing is 41004 Next nothing is 13430 Next nothing is 381 Next nothing is 8755 Next nothing is 58832 Next nothing is 92450 Next nothing is 64821 Next nothing is 84642 Next nothing is 2105 Next nothing is 17305 Next nothing is 60154 Next nothing is 77752 Next nothing is 39994 Next nothing is 30257 Next nothing is 60804 Next nothing is 8060 Next nothing is 83168 Next nothing is 81281 Next nothing is 29319 Next nothing is 84291 Next nothing is 2134 Next nothing is 12889 Next nothing is 70671 Next nothing is 53206 Next nothing is 3864 Next nothing is 71232 Next nothing is 35128 Next nothing is 45383 Next nothing is 43864 Next nothing is 83422 Next nothing is 45657 Next nothing is 90923 Next nothing is 93059 Next nothing is 82431 Next nothing is 32308 Next nothing is 94968 Next nothing is 19185 Next nothing is 55937 Next nothing is 53897 Next nothing is 62026 Next nothing is 63032 Next nothing is 81488 Next nothing is 8601 Next nothing is 73718 Next nothing is 22577 Next nothing is 99460 Next nothing is 58858 Next nothing is 65384 Next nothing is 13622 Next nothing is 14212 Next nothing is 90949 Next nothing is 19069 Next nothing is 48930 Next nothing is 48909 Next nothing is 55205 Next nothing is 51696 Next nothing is 11560 Next nothing is 98848 Next nothing is 82785 Next nothing is 85828 Next nothing is 74671 Next nothing is 47269 Next nothing is 87933 Next nothing is 83248 Next nothing is 9002 Next nothing is 55287 Next nothing is 81455 Next nothing is 47329 Next nothing is 16738 Next nothing is 68790 Next nothing is 31232 Next nothing is 98034 Next nothing is 45243 Next nothing is 43597 Next nothing is 98246 Next nothing is 36542 Next nothing is 54606 Next nothing is 9301 Next nothing is 87282 Next nothing is 48551 Next nothing is 91285 Next nothing is 11090 Next nothing is 71454 Next nothing is 65829 Next nothing is 44481 Next nothing is 23672 Next nothing is 34339 Next nothing is 90980 Next nothing is 78901 Next nothing is 3423 Next nothing is 37522 Next nothing is 19932 Next nothing is 92578 Next nothing is 12931 Next nothing is 12677 Next nothing is 819 Next nothing is 6125 Next nothing is 53248 Next nothing is 14469 Next nothing is 64440 Next nothing is 35616 Next nothing is 67375 Next nothing is 49214 Next nothing is 66203 Next nothing is 80968 Next nothing is 79396 Next nothing is 37781 Next nothing is 9715 Next nothing is 24581 Next nothing is 63414 Next nothing is 63949 Next nothing is 46595 Next nothing is 9384 Next nothing is 64974 Next nothing is 94277 Next nothing is 27064 Next nothing is 55621 Next nothing is 69222 Next nothing is 70168 Next nothing is 63528 Next nothing is 80304 Next nothing is 94510 Next nothing is 40068 Next nothing is 86847 Next nothing is 4861 Next nothing is 91570 Next nothing is 18108 Next nothing is 92797 Next nothing is 10653 Next nothing is 58973 Next nothing is 65708 Next nothing is 85683 Next nothing is 58791 Next nothing is 84134 Next nothing is 16715 Next nothing is 4130 Next nothing is 8333 Next nothing is 2658 Next nothing is 29888 Next nothing is 92509 Next nothing is 24885 Next nothing is 85346 Next nothing is 88754 Next nothing is 87449 Next nothing is 80326 Next nothing is 41417 Next nothing is 62569 Next nothing is 34147 Next nothing is 37638 Next nothing is 28177 Next nothing is 59599 Next nothing is 98155 Next nothing is 1035 Next nothing is 88241 Next nothing is 12774 Next nothing is 88944 Next nothing is 19873 Next nothing is 8308 Next nothing is 46992 Next nothing is 83493 Next nothing is 49337 Next nothing is 10134 Next nothing is 89272 Next nothing is 21051 Next nothing is 10771 Next nothing is 80834 Next nothing is 89357 Next nothing is 14114 Next nothing is 31302 Next nothing is 51107 Next nothing is 23249 Next nothing is 91555 Next nothing is 20144 Next nothing is 57011 Next nothing is 61058 Next nothing is 59316 Next nothing is 109 Next nothing is 22645 Next nothing is 83445 Next nothing is 66015 Next nothing is 82896 Next nothing is 33160 Next nothing is 80234 Next nothing is 69853 Next nothing is 17729 Next nothing is 20261 Next nothing is 95032 Next nothing is 19899 Next nothing is 43848 Next nothing is 53819 Next nothing is 1946 Next nothing is 75651 Next nothing is 62387 Next nothing is 70384 Next nothing is 68507 Next nothing is 88256 Next nothing is 29789 Next nothing is 83043 Next nothing is 14979 Next nothing is 97422 Next nothing is 96333 Next nothing is 665 Next nothing is 83779 Next nothing is 15008 Next nothing is 11812 Next nothing is 32086 Next nothing is 19311 Next nothing is 38267 Next nothing is 7440 Next nothing is 8587 Next nothing is 84193 Next nothing is 19413 Next nothing is 47199 Next nothing is 98913 Next nothing is 79128 Next nothing is 1447 Next nothing is 19812 Next nothing is 7520 Next nothing is 58170 Next nothing is 78767 Next nothing is 36504 Next nothing is 12155 Next nothing is 53725 Next nothing is 15724 Next nothing is 31280 Next nothing is 531 Next nothing is 21899 Next nothing is 21132 Next nothing is 45275 Next nothing is 14983 Next nothing is 9207 Next nothing is 82137 Next nothing is 13076 Next nothing is 32752 Next nothing is 94091 Next nothing is 91316 Next nothing is 17593 Next nothing is 86692 Next nothing is 83916 Next nothing is 84429 Next nothing is 6123 Next nothing is 52149 Next nothing is 35368 Next nothing is 85722 Next nothing is 84034 Next nothing is 71297 Next nothing is 226 Next nothing is 20433 Next nothing is 83398 Next nothing is 5442 Next nothing is 81622 Next nothing is 93237 Next nothing is 76689 Next nothing is 67773 Next nothing is 16764 Next nothing is 12187 Next nothing is 45084 Next nothing is 23959 Next nothing is 83235 Next nothing is 38733 Next nothing is 55868 Next nothing is 29 Next nothing is 83831 Next nothing is 80100 Next nothing is 14723 Next nothing is 60832 Next nothing is 72946 Next nothing is 37493 Next nothing is 27258 Next nothing is 67542 Next nothing is 28426 Next nothing is 35520 Next nothing is 19478 Next nothing is 53315 Next nothing is 89835 Next nothing is 45636 Next nothing is 84411 Next nothing is 44013 Next nothing is 67751 Next nothing is 94119 Next nothing is 15872 Next nothing is 97486 Next nothing is 4530 Next nothing is 2285 Next nothing is 48179 Next nothing is 36594 Next nothing is 52069 Next nothing is 44214 Next nothing is 50187 Next nothing is 63191 Next nothing is 96123 Next nothing is 37905 Next nothing is 35302 Next nothing is 26074 Next nothing is 24510 Next nothing is 77136 Next nothing is 34296 Next nothing is 29704 Next nothing is 22944 Next nothing is 10945 Next nothing is 73256 Next nothing is 40940 Next nothing is 2718 Next nothing is 94666 Next nothing is 53811 Next nothing is 75159 Next nothing is 14802 Next nothing is 29858 Next nothing is 79484 Next nothing is 55887 Next nothing is 8798 Next nothing is 89519 Next nothing is 60659 Next nothing is 53544 Next nothing is 8514 Next nothing is 97430 Next nothing is 1228 Next nothing is 16133 Next nothing is 19944 Next nothing is 25547 Next nothing is 76840 Next nothing is 57327 Next nothing is 96467 Next nothing is 97937 Next nothing is 22345 Next nothing is 75533 Next nothing is 80499 Next nothing is 39170 Next nothing is 25194 Next nothing is 78713 Next nothing is 7331 Next nothing is 92642 Next nothing is 45567 Next nothing is 58155 Next nothing is 4918 Next nothing is 92414 Next nothing is 42478 Next nothing is 57589 Next nothing is 46276 Next nothing is 28529 Next nothing is 36871 Next nothing is 44875 Next nothing is 66783 Next nothing is 1043 Next nothing is 78311 Next nothing is 65698 Next nothing is 95457 Next nothing is 28935 Next nothing is 58156 Next nothing is 12445 Next nothing is 21019 Next nothing is 81654 Next nothing is 82814 Next nothing is 75481 Next nothing is 41055 Next nothing is 55067 Next nothing is 49768 Next nothing is 7422 Next nothing is 30983 Next nothing is 81931 Next nothing is 45122 Next nothing is 75101 Next nothing is 71773 Next nothing is 52323 Next nothing is 41386 Next nothing is 36729 Next nothing is 24027 Next nothing is 82430 Next nothing is 53824 Next nothing is 75518 Next nothing is 38146 Next nothing is 27401 Next nothing is 1321 Next nothing is 20200 Next nothing is 30934 Next nothing is 43923 Next nothing is 9472 Next nothing is 87617 Next nothing is 7973 Next nothing is 40413 Next nothing is 34242 Next nothing is 51833 Next nothing is 7298 Next nothing is 26372 Next nothing is 22704 Next nothing is 87196 Next nothing is 1866 Next nothing is 10454 Next nothing is 8810 Next nothing is 14074 Next nothing is 77163 Next nothing is 15394 Next nothing is 5630 Next nothing is 17801 Next nothing is 80920 Next nothing is 22571 Next nothing is 25311 Next nothing is 270 Next nothing is 2144 Next nothing is 63031 Next nothing is 82371 Next nothing is 94758 Next nothing is 38188 Next nothing is 34663 Next nothing is 69757 Next nothing is 37718 Next nothing is 55552 Next nothing is 81137 Next nothing is 27652 Next nothing is 87492 Next nothing is 25632 Next nothing is 95731 Next nothing is 53233 Next nothing is 69684 Next nothing is 75600 Next nothing is 84135 Next nothing is 72452 Next nothing is 73281 Next nothing is 45808 Next nothing is 934 Next nothing is 12130 Next nothing is 15750 Next nothing is 69210 Next nothing is 99905 Next nothing is 63643 Next nothing is 47850 Next nothing is 83564 Next nothing is 77241 Next nothing is 75978 Next nothing is 24255 Next nothing is 89197 Next nothing is 42934 Next nothing is 76920 Next nothing is 29994 Next nothing is 45675 Next nothing is 30076 Next nothing is 69280 Next nothing is 70557 Next nothing is 43708 Next nothing is 62628 Next nothing is 89095 Next nothing is 80504 Next nothing is 76887 Next nothing is 27720 Next nothing is 67628 Next nothing is 11080 Next nothing is 37351 Next nothing is 55940 Next nothing is 56000 Next nothing is 79866 Next nothing is 33203 Next nothing is 16228 Next nothing is 8716 Next nothing is 74862 Next nothing is 32459 Next nothing is 58183 Next nothing is 18633 Next nothing is 40377 Next nothing is 70141 Next nothing is 84619 Next nothing is 28850 Next nothing is 45925 Next nothing is 7057 Next nothing is 16792 Next nothing is 54610 Next nothing is 21249 Next nothing is 15667 Next nothing is 91038 Next nothing is 44221 Next nothing is 992 Next nothing is 8700 Next nothing is 45100 Next nothing is 68628 Next nothing is 67824 Next nothing is 46145 Collect the comments.
最后得到Collect the comments,再查看zipfile模块,发现ZipFile.getinfo()返回一个ZipInfo对象,包含了文档中相应的文件信息,里面有comment,表示文档说明。
修改上述代码
import zipfile import re filename = 'C:\Python27\channel.zip' def process(): file_list = zipfile.ZipFile(filename) regex = re.compile(r'(\d+)$') nothing = '90052' comments = [] try: while True: fn = '%s.txt' % nothing comments.append(file_list.getinfo(fn).comment) nothing = regex.search(file_list.read(fn, 'r')).group() except: print ''.join(comments) if __name__ == '__main__': process()
把comment都存在一个list中,然后最后输出所有的comment。得到如下的图形
**************************************************************** ** ** ** OO OO XX YYYY GG GG EEEEEE NN NN ** ** OO OO XXXXXX YYYYYY GG GG EEEEEE NN NN ** ** OO OO XXX XXX YYY YY GG GG EE NN NN ** ** OOOOOOOO XX XX YY GGG EEEEE NNNN ** ** OOOOOOOO XX XX YY GGG EEEEE NN ** ** OO OO XXX XXX YYY YY GG GG EE NN ** ** OO OO XXXXXX YYYYYY GG GG EEEEEE NN ** ** OO OO XX YYYY GG GG EEEEEE NN ** ** ** **************************************************************** **************************************************************
it's in the air. look at the letters.
还没结束,我们再回看输出,得到组成hockey的字母组合是oxygen。
url结尾单词改为oxygen,跳转到下一个puzzle。
####################################################################################################################################
以下zipfile模块的内容为转载
class zipfile.ZipFile(file[, mode[, compression[, allowZip64]]])
创建一个ZipFile对象,表示一个zip文件。参数file表示文件的路径或类文件对象(file-like object);参数mode指示打开zip文件的模式,默认值为‘r‘,表示读已经存在的zip文件,也可以为‘w‘或‘a‘,‘w‘表示新建一个zip文档或覆盖一个已经存在的zip文档,‘a‘表示将数据附加到一个现存的zip文档中。参数compression表示在写zip文档时使用的压缩方法,它的值可以是zipfile. ZIP_STORED 或zipfile. ZIP_DEFLATED。如果要操作的zip文件大小超过2G,应该将allowZip64设置为True。
ZipFile还提供了如下常用的方法和属性:
ZipFile.getinfo(name)
获取zip文档内指定文件的信息。返回一个zipfile.ZipInfo对象,它包括文件的详细信息。
ZipFile.infolist()
获取zip文档内所有文件的信息,返回一个zipfile.ZipInfo的列表。
ZipFile.namelist()
获取zip文档内所有文件的名称列表。
ZipFile.extract(member[,path[,pwd]])
将zip文档内的指定文件解压到当前目录。参数member指定要解压的文件名称或对应的ZipInfo对象;参数path指定了解析文件保存的文件夹;参数pwd为解压密码。下面一个例子将保存在程序根目录下的txt.zip内的所有文件解压到D:/Work目录:
import zipfile, os zipFile = zipfile.ZipFile(os.path.join(os.getcwd(), 'txt.zip')) for file in zipFile.namelist(): zipFile.extract(file, r'd:/Work') zipFile.close()
ZipFile.extractall([path[, members[, pwd]]])
解压zip文档中的所有文件到当前目录。参数members的默认值为zip文档内的所有文件名称列表,也可以自己设置,选择要解压的文件名称。
ZipFile.printdir()
将zip文档内的信息打印到控制台上。
ZipFile.setpassword(pwd)
设置zip文档的密码。
ZipFile.read(name[, pwd])
获取zip文档内指定文件的二进制数据。下面的例子演示了read()的使用,zip文档内包括一个txt.txt的文本文件,使用read()方法读取其二进制数据,然后保存到D:/txt.txt
#coding=gbk import zipfile, os zipFile = zipfile.ZipFile(os.path.join(os.getcwd(), 'txt.zip')) data = http://www.mamicode.com/zipFile.read('txt.txt') >ZipFile.write(filename[, arcname[, compress_type]])
将指定文件添加到zip文档中。filename为文件路径,arcname为添加到zip文档之后保存的名称, 参数compress_type表示压缩方法,它的值可以是zipfile. ZIP_STORED 或zipfile. ZIP_DEFLATED。下面的例子演示了如何创建一个zip文档,并将文件D:/test.doc添加到压缩文档中。
import zipfile, os zipFile = zipfile.ZipFile(r'D:/test.zip'), 'w') zipFile.write(r'D:/test.doc', 'ok.doc', zipfile.ZIP_DEFLATED) zipFile.close()ZipFile.writestr(zinfo_or_arcname, bytes)
writestr()支持将二进制数据直接写入到压缩文档。
Class ZipInfo
上面的那个问题中,运用到了ZipInfo对象的相应属性:
ZipFile.getinfo(name) 方法返回的是一个ZipInfo对象,表示zip文档中相应文件的信息。它支持如下属性:
ZipInfo.filename: 获取文件名称。
ZipInfo.date_time: 获取文件最后修改时间。返回一个包含6个元素的元组:(年, 月, 日, 时, 分, 秒)
ZipInfo.compress_type: 压缩类型。
ZipInfo.comment: 文档说明。
ZipInfo.extr: 扩展项数据。
ZipInfo.create_system: 获取创建该zip文档的系统。
ZipInfo.create_version: 获取 创建zip文档的PKZIP版本。
ZipInfo.extract_version: 获取 解压zip文档所需的PKZIP版本。
ZipInfo.reserved: 预留字段,当前实现总是返回0。
ZipInfo.flag_bits: zip标志位。
ZipInfo.volume: 文件头的卷标。
ZipInfo.internal_attr: 内部属性。
ZipInfo.external_attr: 外部属性。
ZipInfo.header_offset: 文件头偏移位。
ZipInfo.CRC: 未压缩文件的CRC-32。
ZipInfo.compress_size: 获取压缩后的大小。
ZipInfo.file_size: 获取未压缩的文件大小。
下面一个简单的例子说明这些属性的意思:
import zipfilezipFile = zipfile.ZipFile('C:\Python27\channel.zip')zipInfo = zipFile.getinfo('90052.txt')print 'filename:', zipInfo.filename print 'date_time:', zipInfo.date_time print 'compress_type:', zipInfo.compress_type print 'comment:', zipInfo.comment print 'extra:', zipInfo.extra print 'create_system:', zipInfo.create_system print 'create_version:', zipInfo.create_version print 'extract_version:', zipInfo.extract_version print 'extract_version:', zipInfo.reserved print 'flag_bits:', zipInfo.flag_bits print 'volume:', zipInfo.volume print 'internal_attr:', zipInfo.internal_attr print 'external_attr:', zipInfo.external_attr print 'header_offset:', zipInfo.header_offset print 'CRC:', zipInfo.CRC print 'compress_size:', zipInfo.compress_size print 'file_size:', zipInfo.file_size zipFile.close() # resultfilename: 90052.txtdate_time: (2006, 6, 6, 6, 6, 6)compress_type: 8comment: *extra: create_system: 0create_version: 20extract_version: 20extract_version: 0flag_bits: 0volume: 0internal_attr: 0external_attr: 0header_offset: 50246CRC: 3984463051compress_size: 23file_size: 21
参考
http://blog.csdn.net/jgood/article/details/4351911
Python challenge 7 - zipfile