<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    xylz,imxylz

    關注后端架構、中間件、分布式和并發(fā)編程

       :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
      111 隨筆 :: 10 文章 :: 2680 評論 :: 0 Trackbacks
    9-20.

    Compressed Files. Write a short piece of code that will compress and decompress gzipped or bzipped files. Confirm your solution works by using the command-line gzip or bzip2 programs or a GUI program like PowerArchiver, StuffIt, and/or WinZip.

     1#!/usr/bin/env python
     2#-*- coding:utf-8 -*-
     3#$Id: p0920.py 166 2010-06-29 03:46:56Z xylz $
     4
     5'''
     6This is a 'python' study plan for xylz.
     7Copyright (C)2010 xylz (www.imxylz.info)
     8'''
     9
    10import gzip
    11import os
    12
    13def compress (dst,f):
    14    print "compress %s to %s" % (f,dst)
    15    f_out = gzip.open(dst,'wb')
    16    f_in = open(f,'rb')
    17    f_out.writelines(f_in)
    18    f_in.close()
    19    f_out.close()
    20    in_size = os.path.getsize(f)
    21    out_size = os.path.getsize(dst)
    22    print "source size %d bytes, target size %d bytes, %d%%" % (in_size,out_size,(out_size*100/in_size))
    23        
    24def decompress (f,dst):
    25    print "decompress %s to %s" % (f,dst)
    26    f_in = gzip.open(f,'rb')
    27    f_out = open(dst,'wb')
    28    f_out.writelines(f_in)
    29    f_in.close()
    30    f_out.close()
    31    in_size = os.path.getsize(f)
    32    out_size = os.path.getsize(dst)
    33    print "source size %d bytes, target size %d bytes, %d%%" % (in_size,out_size,(in_size*100/out_size))
    34    
    35
    36if __name__ == '__main__':
    37    import sys
    38    if len(sys.argv)<3 or ('c'!= sys.argv[1and 'x'!= sys.argv[1]):
    39        print "Usage: %s <c|x> <file> [destfile]" % (sys.argv[0],)
    40        sys.exit(0)
    41    src_file = sys.argv[2]
    42    dest_file = None
    43    if len(sys.argv)>3:
    44        dest_file = sys.argv[3]
    45    
    46
    47    if 'c' == sys.argv[1]:
    48        if not dest_file:
    49            dest_file = src_file+".gz"
    50        compress(dest_file,src_file)
    51    
    52    else:
    53        if not dest_file:
    54            dest_file = src_file[:src_file.index('.gz')] #raise exception if file error
    55        decompress(src_file,dest_file)
    56        
    57        
    58    
    59
    下面是一次執(zhí)行的輸出結果:
    C:\Users\xylz\Desktop\core_python>python p0920.py
    Usage: p0920.py <c|x> <file> 
    [destfile]

    C:\Users\xylz\Desktop\core_python>python p0920.py c p0920.py
    compress p0920.py to p0920.py.gz
    source size 
    1587 bytes, target size 614 bytes, 38%

    C:\Users\xylz\Desktop\core_python>python p0920.py x p0920.py.gz p0920.py
    .2
    decompress p0920.py.gz to p0920.py
    .2
    source size 
    614 bytes, target size 1587 bytes, 38%

    C:\Users\xylz\Desktop\core_python>md5sum p0920.py p0920.py
    .2
    3383e1a05fbede400dd016feed8a55bf *p0920.py
    3383e1a05fbede400dd016feed8a55bf *p0920.py
    .2


    ©2009-2014 IMXYLZ |求賢若渴
    posted on 2010-06-29 11:51 imxylz 閱讀(16701) 評論(0)  編輯  收藏 所屬分類: Python

    ©2009-2014 IMXYLZ
    主站蜘蛛池模板: 亚洲AV无码一区二区三区电影 | 亚洲一区二区三区在线观看蜜桃| 亚洲欧洲高清有无| 国产精品亚洲一区二区三区久久| 国产精品无码永久免费888| 57pao国产成永久免费视频| 亚洲av激情无码专区在线播放| 亚洲日韩久久综合中文字幕| 久久国产免费直播| 亚洲va国产va天堂va久久| 无码精品一区二区三区免费视频 | 免费观看日本污污ww网站一区| 亚洲欧洲自拍拍偷午夜色无码| 亚洲乱码在线卡一卡二卡新区| 国产日韩一区二区三免费高清| 国产色婷婷精品免费视频| 18gay台湾男同亚洲男同| 免费观看国产网址你懂的| 亚洲 欧洲 日韩 综合在线| 久久成人免费大片| 亚洲精品国产高清不卡在线| 免费无码又爽又刺激一高潮| 国产成人高清精品免费软件| jizz免费观看| 91亚洲自偷手机在线观看| 成年人性生活免费视频| 一级毛片视频免费观看| 久久久久亚洲精品成人网小说| 国产成人自产拍免费视频| 亚洲成a人片在线观看日本| 一级毛片一级毛片免费毛片| 免费高清资源黄网站在线观看| 亚洲欧洲精品国产区| 国产成人高清精品免费鸭子| 永久免费av无码入口国语片| 亚洲人成电影青青在线播放| 91成人免费观看| 国产亚洲Av综合人人澡精品| 日本视频免费在线| 两个人看的www免费| 亚洲中文字幕无码中文字在线|