How do you make manifest paths relative to a shared dataset root?

relative_to removes a shared leading path by component. Its default mode requires the path to lie lexically under the given root.

Python example and interpretation

Store labels/1.json for an item under /datasets/a so the tree can move together. A pure path operation does not resolve symbolic links.

from pathlib import PurePosixPath as P
str(P('/datasets/a/labels/1.json').relative_to('/datasets/a'))

Result 'labels/1.json'

中文

导出可搬移的数据集清单时怎样去掉共同根目录?

relative_to 按路径组件计算相对于指定根的路径,默认要求该路径以指定根开始。

Python 算例与适用条件

将 /datasets/a/labels/1.json 记录为 labels/1.json,搬移整个目录后仍可重建引用。纯路径运算不解析符号链接。

from pathlib import PurePosixPath as P
str(P('/datasets/a/labels/1.json').relative_to('/datasets/a'))

结果 'labels/1.json'

References / 参考来源

Keep reading / 继续阅读