#-*- 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中能够正常展现图片。