天下脸皮共十分
我占八分

Python版autoSummary脚本

import os


def getFileStructure(currentFile, father, tab):
    workfilerename(currentFile)
    basename = os.path.basename(currentFile)
    if os.path.isfile(currentFile) and os.path.basename(currentFile).endswith(".md"):
        newName = basename.replace(".md", "")
        return father + "* [" + newName + "](" + currentFile + ")"
    elif os.path.isdir(currentFile) and workfile(currentFile) == True:
        fatherOrigin = "* [" + basename + "]()";
        files = os.listdir(currentFile)
        absolutPath = os.path.relpath(currentFile, ".")
        if len(files) == 0:
            return None
        subStructure = []
        for file in files:
            newTab = tab + '    '
            fileStructure = getFileStructure(absolutPath + "\\" + file, "", newTab)
            if fileStructure != None:
                subStructure.append(fileStructure)
        if len(subStructure) == 0:
            return None
        for ele in subStructure:
            fatherOrigin = fatherOrigin + "\n" + tab + ele
        return fatherOrigin


def workfile(mypath):
    for root, dirs, files in os.walk(mypath):
        flag = False
        for name in files:
            if name.endswith(".md"):
                flag = True
                return flag
        return flag

def workfilerename(mypath):
    for root, dirs, files in os.walk(mypath):
        for name in files:
            if "(" in name or ")" in name:
                newName = name.replace("(", "(").replace(")", ")")
                namePath = os.path.join(root, name)
                newNamePath = os.path.join(root, newName)
                os.rename(namePath, newNamePath)
        for dir in dirs:
            if "(" in dir or ")" in dir:
                newDir = dir.replace("(", "(").replace(")", ")")
                dirPath = os.path.join(root, dir)
                newDirPath = os.path.join(root, newDir)
                os.rename(dirPath, newDirPath)



if __name__ == '__main__':
    workfilerename(".")
    path = os.path.curdir
    current = os.listdir(path)
    content = []
    toBeWrite = ''
    for currentFile in current:
        workfilerename(currentFile)
        structure = getFileStructure(currentFile, "", "  ")
        if structure != None:
            content.append(structure)
    print(content)
    summary = open("SUMMARY.md", 'w', encoding='UTF-8')
    for str in content:
        toBeWrite = toBeWrite + '\n' + str
    summary.write(toBeWrite)
    summary.close()
赞(3) 打赏
未经允许不得转载:Stephen Young » Python版autoSummary脚本
分享到: 更多 (0)

评论 抢沙发

评论前必须登录!

 

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏