1

I've got in the habit of using Pathlib all the time instead of os. I find the code is more concise and it seems better and more powerful to use an object oriented approach rather than manipulate strings which os does.

But most code I read still uses os module, including PyTorch itself. Why is this? Are there speed advantages of using os? Or is it just a convention which is taking a long time to change? Or something else?

This answer describes high level differences between os and Pathlib but doesn't explain why libraries make the choices they have.

  • 4
    "it seems better and more powerful to use an object oriented approach rather than manipulate strings which os does" - that's your opinion. Not everyone shares it. – user2357112 Aug 24 '22 at 08:17
  • 3
    probably because path-like objects aren't as versatile as using the os module which simply returns strings. And probably because by removing an added layer of abstraction it is more effecient – Alexander Aug 24 '22 at 08:17
  • 1
    Probably because two factors. The first one is legacy code compatibility and the second one is compatibility with other non python tools that accept strings as a valid format and it is easier to use it. Pathlib is useful for some cases, you should use it very often, but strings are more generic and common in higher level modules of generic libraries. Pathlib is more common on lower level components that are not so visible to end users. And, for machine learning, most of the code you are using is written in C++ under the hood. I think this is maybe another reason, but I am not sure of this – Fran Arenas Aug 24 '22 at 08:19

0 Answers0