-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
40 lines (33 loc) · 1013 Bytes
/
utils.py
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
#!/usr/bin/env python3
import os
import pathlib
def get_full_path(path: str) -> pathlib.Path:
"""
Expands a relative path to absolute, and
resolves symlinks if needed
:param path: The path to check
:return: An absolute path
"""
path = pathlib.Path(path)
if not path.is_absolute() or path.is_symlink():
path = path.resolve()
return path
def is_valid_path(path: str, strict=False) -> bool:
"""
Checks if a path is valid, ie
if a file or directory exists, and
if it is readable/writable
:param path: The path
:param strict: Don't allow path to already exist
:return: If the path is valid
"""
path = pathlib.Path(path)
if path.exists() and not strict:
if path.is_file():
return os.access(path, os.R_OK) # read
elif path.is_dir():
return os.access(path, os.W_OK) # write
elif not path.exists() and strict:
return True
else:
return False