]>
Commit | Line | Data |
---|---|---|
1 | import os | |
2 | from typing import Union | |
3 | ||
4 | _Path = Union[str, os.PathLike] | |
5 | ||
6 | ||
7 | def ensure_directory(path): | |
8 | """Ensure that the parent directory of `path` exists""" | |
9 | dirname = os.path.dirname(path) | |
10 | os.makedirs(dirname, exist_ok=True) | |
11 | ||
12 | ||
13 | def same_path(p1: _Path, p2: _Path) -> bool: | |
14 | """Differs from os.path.samefile because it does not require paths to exist. | |
15 | Purely string based (no comparison between i-nodes). | |
16 | >>> same_path("a/b", "./a/b") | |
17 | True | |
18 | >>> same_path("a/b", "a/./b") | |
19 | True | |
20 | >>> same_path("a/b", "././a/b") | |
21 | True | |
22 | >>> same_path("a/b", "./a/b/c/..") | |
23 | True | |
24 | >>> same_path("a/b", "../a/b/c") | |
25 | False | |
26 | >>> same_path("a", "a/b") | |
27 | False | |
28 | """ | |
29 | return os.path.normpath(p1) == os.path.normpath(p2) |