• 利用python生成gitbook目录文件


    将 summary-generator.py 放在已知根目录下。

    开发环境:

    • macOS
    • python 3

    完整代码:

    #!/usr/bin/python
    # -*- coding: UTF-8 -*-
    
    import os, sys
    
    path = "."
    result_list = []
    result_str = ""
    flag = "* "
    tab = "  "
    # 定义递归方法
    def find_files(path, n = 0, dir = ""):
        global result_str
        if os.path.isdir(path):
            if path <> "node_modules" and path <> "_book" and path <> ".DS_Store":
                if path <> ".":
                    dir += path + "/"
                # path是文件夹
                if path <> ".":
                    result_list.append(tab * n + flag + "[" + path + "](" + dir + "/README.md" + ")")
                    result_str += tab * n + flag + "[" + path + "](" + dir + "/README.md"  + ")
    "
                    n = n + 1
                else:
                    n = 0
                # 按文件名称排序
                dirs = sorted(os.listdir(path))
                for file in dirs:
                    find_files(file, n, dir)
        else:
            # path是文件
            if path.endswith(".md") and path <> "SUMMARY.md" and path <> "README.md":
                result_list.append(tab * n + flag + "[" + path + "](" + dir + path + ")")
                result_str += tab * n + flag + "[" + path + "](" + dir + path + ")
    "
    find_files(".", 0, "")
    for file in result_list:
        print file
    try:
        f = open('./SUMMARY.md', 'w')
        f.write(result_str)
    finally:
        if f:
            f.close()
    
  • 相关阅读:
    LC 377. Combination Sum IV
    LC 718. Maximum Length of Repeated Subarray
    使用 Synchronized 关键字
    线程的基本概念
    谈谈 JAVA 的对象序列化
    JAVA 注解的基本原理
    基于 CGLIB 库的动态代理机制
    基于 JDK 的动态代理机制
    反射的基本原理
    泛型的基本原理
  • 原文地址:https://www.cnblogs.com/longying2008/p/15068115.html
Copyright © 2020-2023  润新知