python脚本整理一下桌面

python脚本整理一下桌面

代码如下:


#!/usr/bin/python
# coding=utf-8
import os
import shutil
import 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 e


def 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 T


if __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")
posted @ 2016-12-30 19:34  学而时习之2  阅读(157)  评论(0编辑  收藏  举报