「Python Recipes」に戻る |
---|
Release: | 1.0 |
---|---|
Date: | November 19, 2010 |
テスト環境
OS: Ubuntu10.10
Python2.6.6
使い勝手悪いかもしれません。¶
Pythonでファイルを圧縮したくてつくりました。
取り敢えず、コメントを見てもらえれば使い方はわかると思います。
スクリプトファイルとして実行するときは、
$ chmod +x pycompress.py
$ ./pycompress.py 圧縮したいファイル (bz2, gz, zip)のうちから1つ
とすれば、簡単に圧縮できるようになっています。
まだ未完成な部分があるので、バグがあったら教えてください。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | #!/usr/bin/env python
#-*- coding:utf-8 -*-
# Author: Iyori Komiyama
# Contact: hazimarino@gmail.co.jp
# site: http://hazimarino.blogspot.com/
"""\
ファイルをtar.bz2, tar.gz または zip で圧縮します。
"""
import os
import tarfile
import zipfile
from fnmatch import fnmatch
exts = ['*.lzh', '*.zip', '*.rar', '*.gz', '*.bz2', '*.xz', '*.?[gbx]z']
def pycompress(cmpfile, types="bz2", min_size=16):
"""\
pycompress(cmpfile[, types[, min_size]]) -> None
cmpfile 圧縮したいファイル名を入力してください。\
(スクリプファイルと同じフォルダでない場合は、絶対パスを入力してください)
types bz2, gz, zip のいずれかの圧縮タイプを入力してください。
min_size 圧縮したくない場合の最低サイズを入力してください。
"""
if os.stat(cmpfile).st_size > min_size:
if not any(fnmatch(cmpfile.lower(), p) for p in exts):
if types == "zip":
zp = zipfile.ZipFile("{0}.zip".format(cmpfile),
'w', zipfile.ZIP_DEFLATED)
zp.write(cmpfile)
zp.close()
else:
tar = tarfile.open('{0}.tar.{1}'.format(cmpfile, types),
'w:{0}'.format(types))
tar.add(cmpfile)
tar.close()
else:
print(u"すでに圧縮ファイルです。".encode('utf8'))
else:
print(u"ファイルが小さすぎます。".encode('utf8'))
if __name__ == '__main__':
import sys
try:
if sys.argv[2] == "bz2":
pycompress(sys.argv[1], 'bz2')
elif sys.argv[2] == "gz":
pycompress(sys.argv[1], 'gz')
elif sys.argv[2] == "zip":
pycompress(sys.argv[1], 'zip')
else:
raise
except (IndexError, TypeError):
print("適切なタイプ(bz2, gz, zip)を入力してください。")
'''
Created on 2010-11-13
@author Iyori Komiyama
'''
|
gist¶
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# Author: Iyori Komiyama | |
# Contact: hazimarino@gmail.co.jp | |
# site: http://hazimarino.blogspot.com/ | |
"""\ | |
A file is compressed by "tar.bz2", "tar.gz" or "zip". | |
""" | |
import os | |
import tarfile | |
import zipfile | |
from fnmatch import fnmatch | |
exts = ['*.lzh', '*.zip', '*.rar', '*.gz', '*.bz2', '*.xz', '*.?[gbx]z'] | |
def pycompress(cmpfile, types="bz2", min_size=16): | |
if os.stat(cmpfile).st_size > min_size: | |
if not any(fnmatch(cmpfile.lower(), p) for p in exts): | |
if types == "zip": | |
zp = zipfile.ZipFile("{0}.zip".format(cmpfile), | |
'w', zipfile.ZIP_DEFLATED) | |
zp.write(cmpfile) | |
zp.close() | |
else: | |
tar = tarfile.open('{0}.tar.{1}'.format(cmpfile, types), | |
'w:{0}'.format(types)) | |
tar.add(cmpfile) | |
tar.close() | |
else: | |
print("It is already a data compressed file.") | |
else: | |
print("The file is too small. ") | |
if __name__ == '__main__': | |
import sys | |
try: | |
if sys.argv[2] == "bz2": | |
pycompress(sys.argv[1], 'bz2') | |
elif sys.argv[2] == "gz": | |
pycompress(sys.argv[1], 'gz') | |
elif sys.argv[2] == "zip": | |
pycompress(sys.argv[1], 'zip') | |
else: | |
raise | |
except (IndexError, TypeError): | |
print("Please input appropriate type (bz2, gz, zip). ") |
「Python Recipes」に戻る |
---|
0 件のコメント:
コメントを投稿