首页 > 编程语言 > 详细

Python脚本--批量更改本地md文件的图片链接

时间:2016-08-12 21:27:36      阅读:310      评论:0      收藏:0      [点我收藏+]
#-*- coding:utf-8 -*-
import re
import os

def patch_md(filename):
    if filename.endswith(.md):
        return True
    return False

def _patch_replace(text):
    pattern=re.compile(r(?<=!)\[.*\]\((.*)(?=\)))
    result = re.findall(pattern,text)for filedir in result:
        if \\ in filedir:
            listfile = filedir.split(\\)
        else:
            listfile = filedir.split(/)
        pattern_file = r(?<=!)\[.*\]\(.*[\\/]+(?=+listfile[-1]+r\))
        pattern_repl=re.compile(pattern_file)
        result = re.findall(pattern_repl,text)
        print result
        replace_file=r[](https://github.com/xxxxxx/img/‘#换成图片所存储的位置
        text=re.sub(pattern_repl,replace_file,text)
    return text

def patch_replace(filename):
    print filename
    with open(filename,r+) as f:
        text=f.read()
        text_file=_patch_replace(text)
    with open(filename,w) as f:
        f.write(text_file)


def run():
    dirpath = raw_input("输入所要处理的文件的目录:")
    fileset = filter(patch_md,os.listdir(dirpath))
    print fileset
    for filename in fileset:
        absfile = os.path.join(dirpath,filename)
        patch_replace(absfile)

if __name__==__main__:
    run()

批量更改本地的md文件中的图片链接。以适应github,使得在github中能够正常展现图片。

Python脚本--批量更改本地md文件的图片链接

原文:http://www.cnblogs.com/zhang-wen/p/5766483.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!