Should you be using Pathlib?

I've been working in Python for four years - recently I made the conscious choice to use pathlib rather than the more familiar os & os.path.

It's been a great experience - I wrote a short piece (friend link) comparing the two libraries on common workflows & tasks done with file paths.

Should you be using Pathlib?

22 comments · 89% upvoted · other discussions

comments

krazybug

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

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

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

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.