小工具:使用Python自动生成MD风格链接

很久之前我在Github上搞了一个LeetCode的仓库,但一直没怎么维护。最近发现自己刷了不少LC的题目了,想搬运到这个仓库上。

玩Github最重要的当然是写README了,MD的逼格决定了项目牛逼不牛逼。但是让我一个一个去手写项目中的链接那是不可能的,这辈子都不可能手写,只有写脚本自动生成才能满足装逼的样子。

import os
import os.path
# 根目录
rootdir="E:/gitTest/LeetCode/"
list=[]
result=[]
# 定义链接前缀
prefix="https://github.com/rever4433/LeetCode/tree/master/"
# 链接中的空格
space="%20"
for parent,dirnames,filenames in os.walk(rootdir):
    for dirname in dirnames:
        # 忽略的文件名
        if dirname == ".git":
            continue
        # 文件夹名放入list
        if os.path.isdir(dirname):
            list.append(dirname)
        
        
    
for i in list:
    linkSuffix=i.replace(" ",space)
    # 生成MD链接,格式:### 1.[Invert Tree]() 
    i="### "+str(list.index(i)+1)+".["+i+"]("+prefix+linkSuffix+")"
    result.append(i)

with open('test.md','w') as fw:
    fw.write('%s'%'\n'.join(result))
    

猜你喜欢

转载自www.cnblogs.com/rever/p/9363454.html