python的sh库使用案例

sh 可让你调用任意程序,就好象是一个函数一般 http://amoffat.github.io/sh/

这里我用unzip以及mv为例:

#coding=utf-8

'''
@CreateDate: Fri Feb 14 15:48:13 CST 2014
@FileName:unzip.py
@Description:负责解压并重命名聊天记录
'''
import os
from sh import unzip,mv
def myunzip():
    '''只负责解压'''
    path = os.getcwd()
    path = "%s/%s" % (path,"qqfile")
    for root,dirs,files in os.walk(path):
        #print root,type(dirs)

        for fn in files:
            if fn.endswith(".zip"):
                zippath = os.path.join(root,fn)
                abs_zippath = os.path.join(root,fn.replace(".zip",""))
                print zippath,abs_zippath
                unzip(zippath,d=abs_zippath)
def rename():
    '''只负责把解压出来的txt重命名'''
    path = os.getcwd()
    path = "%s/%s" % (path,"qqfile")
    for root,dirs,files in os.walk(path):
        for fn in files:
            if fn.endswith(".txt"):
                filename = root.split("/")[-1]
                old = os.path.join(root,fn)
                new = "%s/%s.txt" % (root,filename)
                mv(old,new)
if __name__ == "__main__":
    myunzip()
    #rename()