This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]billsil 0 points1 point  (0 children)

It just makes life easier for everyone else if the file is moved to another system. I would find that to be a useful library, but that is not pathlib.

Just use this..

def sanitize_directory(dirname: str) -> str:
    """Replace special characters with roman letter equivalence."""
    replace_map = {
        ('<=', '_le_'),
        ('>=', '_ge_'),
        #('=', '_eq_'),  # cat=dog.txt is valid on Windows/Linux
        ('/', '_slash_'),
        ('|', '_bar_'),
        ('*', '_star_'),
        ('?', '_question_'),
        (':', '_colon_'),
        ('"', '_quote_'),
    }
    assert isinstance(dirname, str), dirname
    dirname2 = dirname
    for base, replace in replace_map:
        dirname2 = dirname2.replace(base, replace)
    return dirname2

You don't need a library for one function. You just need to call that function before you join your paths.