I want to fix this function so that special characters are replaced with a similar legal character or in case that's too complex removed depending on the os.
Is there a simple way to do that or should I just do something like?
value = "".join(c for c in meta[key] if c.isalnum() or c in " -._")
value = " ".join(value.split())
def build_folder_tree(tree: str, meta: Dict) -> str:
"""
Build a folder tree from a metadata dictionary
"""
path = ""
for key in tree.split(os.sep):
if key not in meta or not meta[key]:
continue
value = meta[key]
if isinstance(value, list):
value = meta[key][0]
path = os.path.join(path, value)
return path
edit: I think I'll use pathvalidate package because it can check automatically for the current os.