KeyDive/extractor/uils.py

24 lines
546 B
Python
Raw Permalink Normal View History

2024-06-22 16:07:30 +00:00
import re
from typing import Union
from pathlib import Path
def sanitize(path: Union[Path, str]) -> Path:
if isinstance(path, str):
path = Path(path)
paths = [path.name, *[p.name for p in path.parents if p.name]][::-1]
for i, p in enumerate(paths):
p = p.replace('...', '').strip()
p = re.sub(r'[<>:"/|?*\x00-\x1F]', '_', p)
paths[i] = p
return Path().joinpath(*paths)
if __name__ == '__main__':
path = Path() / 'hello rgtgr/sdg'
print(path)
path = sanitize(path)
print(path)