""" 本文件用于处理markdown文件的移动问题,尤其是对于相对路径的移动有一定的自动化处理能力。 思路是:如果有md文件需要移动/复制的话, - 检索md文件中的绝对链接和相对链接; - 将这些相对链接的文件放在新的目标位置; - 如果目标位置已经有同名文件+同文件大小,跳过;否则提醒是跳过、保留且修改还是怎样。 """ import re import os import shutil import urllib.parse def find_links(filepath: str): """ 查找md文件中的超链接,并返回绝对链接和相对链接(列表)。 其中的相对路径开头没有斜杠,所以要手动补充斜杠获取绝对路径。 :param filepath: 文件完整路径 :return: Tuple(res_abs,res_rel): 绝对链接(列表),相对链接(列表) """ pattern1 = re.compile(r'= 0: if str(pattern_head.findall(p)[0]).find(':') >= 0: res_abs.append(p) else: res_rel.append(p) else: res_rel.append(p) except Exception as e: print(p) print(e) return res_abs, res_rel def copy_md_linked_files(file_path_old: str, new_path, mode='copy'): """ :param file_path_old: md文件所在完整路径(包括文件名) :param new_path: md文件目标位置(文件夹) :param mode: 移动或者复制 :return: """ lst_links_abs, lst_links_rel = find_links(file_path_old) fpath, fname = os.path.split(file_path_old) for p in lst_links_rel: # URL解码 p = urllib.parse.unquote(p) # old_pth = fpath.replace('\\', '/') + '/' + p # 原位置 print(old_pth) tar_pth = new_path.replace('\\', '/') if tar_pth.endswith('/'): tar_pth = tar_pth + p else: tar_pth = tar_pth + '/' + p print(tar_pth) tarp, tarfn = os.path.split(tar_pth) try: if not os.path.exists(tarp): os.makedirs(tarp) shutil.copyfile(old_pth, tar_pth) # 目前,附件永远是复制,因为移动可能存在其他问题 except Exception as e: print(e) def copy_md(file_path_old: str, file_path_new: str, mode='copy'): """ 复制md文件到指定位置。 :param file_path_old: :param file_path_new: 目标位置(文件夹 + 文件名) :param mode: 移动或者复制 :return: 无 """ # 相对路径附件,复制到目标位置 new_path, new_filename = os.path.split(file_path_new) copy_md_linked_files(file_path_old, new_path) # if len(new_filename) == 0: fpath, fname = os.path.split(file_path_old) new_full_path = new_path + '/' + fname else: new_full_path = file_path_new try: if mode == 'copy': shutil.copyfile(file_path_old, new_full_path) elif mode == 'move': shutil.move(file_path_old, new_full_path) except Exception as e: print('复制文件出错:') print(e) def mv_md(file_path_old: str, file_path_new: str): """ 移动文件 """ copy_md(file_path_old, file_path_new, mode='move') if __name__ == '__main__': file_old = r"D:\MaJian\Desktop\@短文剪辑\Python 图像库 PIL 的类 Image 及其方法介绍_leemboy 的博客 - CSDN 博客_pil.md" file_new = r'd:/to_delete/a/b/c/d\\e/PIL.md' # pth_abs, pth_rel = find_links(fip) # print(pth_abs) # print(pth_rel) # # copy_md_linked_files(fip, 'd:/to_delete') copy_md(file_old, file_new)