免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 5411 | 回复: 0
打印 上一主题 下一主题

Zip应用 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-02-02 18:22 |只看该作者 |倒序浏览

                                                                Level 6ZIP document: http://docs.python.org/library/zipfile.html
图片中有一条牛仔裤(拉链的英文名为:zipper)。
Title提示为pair.
源码中有〈!-- 〈-- zip --〉字样。
从http://www.pythonchallenge.com/pc/def/channel.zip 下载zip文件
解压这个ZIP文件,里面有一个readme文件提示:
welcome to my zipped list.
hint1: start from 90052
hint2: answer is inside the zip
从这里可以看出跟Level 4差不多,就是反复从文件中提取下一文件的文件名。
               
               
                import os
import sys
import re
def get_nextfile(fPath):
    src=open(fPath).read()
    num=re.search('\d+',src)
    if not num:
        print src
        return num.group(1)
    else:
        return num.group(0)
if __name__=='__main__':
    pDir='E:\python challenge\channel'+'\\'
    fileName='90052.txt'
    fPath=pDir+fileName
    for n in range(1000):
        print n
        num=get_nextfile(fPath)
        fPath=pDir+num+'.txt'
        print fPath
   
执行后显示:
collect the comments
这个comments 就在ZIP文件里面,ZIP有一个对像叫ZipFile.comment。
ZipFile.commentThe comment text associated with the ZIP file.  If assigning a comment to a
ZipFile
instance created with mode ‘a’ or ‘w’, this should be a
string no longer than 65535 bytes.  Comments longer than this will be
truncated in the written archive when
ZipFile.close()
is called.
               
                import sys
import os
import re
import zipfile
if __name__=='__main__':
    comment=[]
    f=zipfile.ZipFile('E:\python challenge\channel.zip','r')
    nothing='90052'
    nextFile=nothing+'.txt'
    #print zipfile.is_zipfile('E:\python challenge\channel.zip')
    while(1):
        try:
            comment.append(f.getinfo(nextFile).comment)
            nothing=re.search('\d+',f.open(nextFile).read()).group()
            nextFile=nothing+'.txt'
        except:
            break
    f.close()  #close zip file
    print ''.join(comment)
也可以利用代码从网站上下载ZIP文件:
               
                from StringIO import StringIO
zobj = StringIO()
zobj.write(urllib.urlopen("http://pythonchallenge.com/pc/def/channel.zip").read())
z = zipfile.ZipFile(zobj)
               
               
               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u3/107101/showart_2167782.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP