pathlib生成文件的软链接
-
在训练深度网络时,保存模型,想要维护一个latest.t7的文件,想到给最好的模型创建一个软链接到latest.t7
-
这样模型不占地,还能便于后续脚本加载最好模型
-
起初是看到mmdetection中是这样做了,找了一下它对应的源码如下:
def symlink(src, dst, overwrite=True, **kwargs):
if os.path.lexists(dst) and overwrite:
os.remove(dst)
os.symlink(src, dst, **kwargs)
- 这个方法的确是可以用的,但是用的是os库,想用pathlib改写一下,其实pathlib中只是对os的库进行了一些封装,本质还是调用的这个
- 简单写了一个函数如下:
def symlink(src_path: Path, dst_path: Path):
if not isinstance(src_path, Path):
src_path = Path(src_path)
if not isinstance(dst_path, Path):
dst_path = Path(dst_path)
if dst_path.is_symlink():
dst_path.unlink()
dst_path.symlink_to(src_path)
-----------------------------------------
你驻足于春色中,于那独一无二的春色之中。