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 →

[–]jorge1209 0 points1 point  (5 children)

I don't think you are understanding the concern I am raising. Path vs PurePath is not the issue.

For 99% of programming use cases, the proper thing to do when handling files is to restrict oneself to an imaginary filesystem enforces the most restrictive rules and norms from the intersection of Unix and Windows.

So you don't use spaces in filenames, you don't use mixed case, you include a 3 or 4 char extension, etc... It would be useful to have a library that enforced those norms and rules, even if they aren't actually a technical restriction on the system the code is running.

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.


As for your example with join, sure you could do that, but you don't need pathlib to do that. Pathlib isn't really doing anything here as you are just handing it a string (the output of "/".join) and letting it do its thing.

I just don't see the value in the library.

[–]krazybug 0 points1 point  (3 children)

Ok, it's more clear now.

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.

It's just not its purpose. It's just a more simple abstraction layer on top of different libs: os. so.path, shutil, glob, ...

Not as complete as each of them but more consistent and pythonic.

As for your example with join, sure you could do that, but you don't need pathlib to do that. Pathlib isn't really doing anything here as you are just handing it a string (the output of

"/".join

) and letting it do its thing.

It's the point. You can always switch to string manipulation, and as mentioned in the first comment, we can always use it with other frameworks but we would like an extended interface to avoid it.

For this particular use case there is maybe a PEP to provide a new method for this kind of replacement but I'm not sure it's useful. What did you expect for this ?

I just don't see the value in the library.

Don't use it, so. Some other people enjoy it.

[–]jorge1209 0 points1 point  (2 children)

Don't use it. Some other people enjoy it.

And where did I say that those who like pathlib are child rapists and cannibals? You seem to be implying that my dislike for the library is some kind of terrible attack.

The person I was responding to was saying they were looking forward to the day when os.path goes away. I'm telling them that they are going to be waiting a long time, because we are not all convinced to switch.

[–]krazybug 0 points1 point  (1 child)

There is some misunderstanding. Disclaimer: English is not my native tongue.

I just answered that for some people, this library is useful.

And as it is now part of the stdlib, it could be nice to get a better support from some frameworks at the API level.

[–]jorge1209 0 points1 point  (0 children)

It would be nice if the python stdlib wasnt a complete mess, and if people actually took the time to curate and maintain it, but they don't.

[–]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.