python脚本整理一下桌面

来源:互联网 发布:美国中文导航软件 编辑:程序博客网 时间:2024/05/22 08:23

python脚本整理一下桌面

代码如下:

#!/usr/bin/python# coding=utf-8import osimport shutilimport re# 取得后缀名是.0的文件def getFileByEndName(FilepathDir, fileEnd):    '''    if in directory (filepathDir),the (fileEnd) is ".txt"    you will get a list[] include ".txt" file paths,    '''    f = []    k = []    FilepathDir = str(FilepathDir)    if FilepathDir == "":  # if the path is NULL ,return        return []    if FilepathDir[-1] != "\\":  # if the last charecter of the path is "\\" ?        FilepathDir = FilepathDir + "\\"    a = os.listdir(FilepathDir)    # print a    b = [x for x in a if os.path.isfile(FilepathDir + x)]    for x in b:        c, d = os.path.splitext(FilepathDir + x)  # 分离文件名和其后缀 分别赋给c和d        if d == fileEnd:  # 如果后缀是'.0'的文件            f.append(FilepathDir + x)  # 逐个加入列表f中    return f  # 返回f[]def CP(d, str):    # L = getFileByEndName("./", str)    L = find("./", str)    if not os.path.exists(d):        os.mkdir(d)    else:        try:            for x in L:                print x                os.system("move %s %s" % (x, d))        except Exception, e:            print edef find(p, str):    T = []    pattern = re.compile(r'[^~$](.*)%s' % (str))    L = os.listdir(p)    for x in L:        match = pattern.match(x)        if match:            T.append(x)    return Tif __name__ == "__main__":    CP("exe", ".exe")    CP("doc", ".docx")    CP("doc", ".doc")    CP("xls", ".xls")    CP("xls", ".xlsx")    CP("ZIP", ".zip")    CP("shell", ".bat")    CP("photos", ".png")    CP("photos", ".jpg")    CP("markdown", "md")
0 0