#-*-coding:utf-8-*-ImportReImportOSdefpatch_md (filename):ifFilename.endswith ('. MD'): returnTruereturnFalsedef_patch_replace (text): Pattern=re.compile (R'(? <=!) \[.*\]\((.*)(?=\))') Result=Re.findall (Pattern,text) forFiledirinchResult: if '\\' inchFiledir: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)Printresult Replace_file=r'[] (https://github.com/xxxxxx/img/' #换成图片所存储的位置text=re.sub (Pattern_repl,replace_file,text) returntextdefpatch_replace (filename):Printfilename with open (filename,'r+') as F:text=f.read () text_file=_patch_replace (text)with open (filename,'W') as F:f.write (text_file)defrun (): Dirpath= Raw_input ("Enter the directory of the files you want to process:") Fileset=Filter (Patch_md,os.listdir (dirpath))PrintFileset forFileNameinchFileset:absfile=Os.path.join (dirpath,filename) patch_replace (absfile)if __name__=='__main__': Run ()
Bulk change the picture link in the local MD file. To adapt to GitHub, allowing images to be displayed normally on GitHub.
Python Script--bulk change the picture link of the local MD file