#函数:检查目录是否存在,不存在则新建 defcheckdir_andcreate(dir_path_set): if dir_path_set[-1:] == "": dir_path_set = dir_path_set[0:-1] if os.path.isdir(dir_path_set) == False: try: os.mkdir(dir_path_set) except: print("Dir create fail") exit() print("Now create a new dir, path:", dir_path_set) #函数:特点字符查找 defcheck_mark(str_line, list_check, mode_check): resault_check = False for i in list_check: if mode_check == True:#模式True:列表中的所有字符都要有,一个都不能少 resault_check = True try: str_line.index(i) except: resault_check = False break elif mode_check == False:#模式False:只要匹配到列表中字符的其中一个就行 resault_check = False state = 0 try: str_line.index(i) state = 1 except: state = 0 if state == 1: resault_check = True break else: resault_check = False print("Function input error") break return resault_check
defread_and_find(file_name): ifTrue: try: withopen(outmddir + file_name[file_name.rfind("/"):], 'w', encoding = 'utf-8') as fout:#在输出目录新建同名文件 file_processing = open(file_name, 'r+', encoding = 'utf-8') #读取原文件 for line in file_processing.readlines():#分行处理 lines = str(line) if check_mark(lines, markdown_setup, True) == Trueand check_mark(lines, markdown_setup_blacklist, False) == False:#检测是否为markdown语法的图片链接,并且无黑名单列表中的字符 http_l = lines.index("](") + 2 http_r = lines.index(")", http_l) pic_http_addr = lines[http_l:http_r] if check_mark(pic_http_addr, download_list, False):#检测网址是否有效 print("String replace:", pic_http_addr ,end='') iflen(replace_source) == len(replace_target): for iii,data_r inenumerate(replace_target): lines = lines.replace(replace_source[iii], data_r)#处理字符 print(" to ", lines) else: print("****************************") print("Error, check (replace_source) and (replace_target)") print("****************************")
fout.write(lines)#写入文本
file_processing.close() returnTrue except: print("Err at read file :", file_name) returnFalse
checkdir_andcreate(mddir) checkdir_andcreate(outmddir) dict_pic_path = [] list = os.listdir(mddir) #列出文件夹下所有的目录与文件 for i inrange(0,len(list)): path = os.path.join(mddir,list[i]) if os.path.isfile(path): str_path = str(path) str_path = str_path[str_path.rfind(".")+1:] for i in ext_name: #忽略不在白名单的扩展名 if str_path == i: #print(path) dict_pic_path.append(path) break
for i in dict_pic_path:#挨个处理文件 print("File:", i) print("-----------------------------------") read_and_find(i) print("-----------------------------------\n")