1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
|
import os import re
def search(search_path, search_result): all_file = os.listdir(search_path) for each_file in all_file: if os.path.isdir(search_path + each_file): search(search_path + each_file + '/', search_result) else: if re.findall('.*\.md$', each_file) == [each_file]: search_result.append(search_path + each_file)
def replace(replace_file_name, replace_old_str, replace_new_str): with open(replace_file_name, "r", encoding = "UTF-8") as f1: content = f1.read() f1.close() t = content.replace(replace_old_str, replace_new_str) with open(replace_file_name, "w", encoding = "UTF-8") as f2: f2.write(t) f2.close()
path_list = [ 'E:/code_zone/hexo-source/source/_posts/', 'E:/code_zone/hexo-source-butterfly/source/_posts/', ] old_str = 'https://cdn.iocdn.cc/npm/mycpen-image-bed@0.0.0-flvvbcbcgo/image/' new_str = 'https://cdn.iocdn.cc/npm/mycpen-image-bed@0.0.0-flvvbcbcgo/image/'
search_result = []
if __name__ == '__main__': result = [] for path in path_list: search(path, result) count = 0 for file_name in result: replace(file_name, old_str, new_str) count += 1 print("{} done {}".format(file_name, count))
|