python的sh库使用案例

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

这里我用unzip以及mv为例:

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

'''
@CreateDate: Fri Feb 14 15:48:13 CST 2014
@FileName:unzip.py
@Description:负责解压并重命名聊天记录
'''
import os
from sh import unzip,mv
def ():
'''只负责解压'''
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()