Python Forum
Is pathlib a viable replacement for os.path?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Is pathlib a viable replacement for os.path?
#1
So far I have used only os.path, and as I have stumbled over that topic in a guide I am reading, it got me wondering...
Can pathlib replace os.path completely, even doing a better job at it maybe?
Reading about os.path reminded me of much praise for pathlib expressed on the forums, with examples shown as well.
Reply
#2
A long time ago (2004 may be) there was a path module written by Jason Orendorff (older version) that many people included in their projects. Various alternative implementations existed such as this one. For personal projects, I had my own version of this module, obtained by adding more and more methods to the Path class.

The main benefit of these classes is the programmer's comfort. It is satisfying to build paths by using the division operator instead of joining strings, it is comfortable to have features available by calling an object's method.

If you look at python's pathlib, you'll notice that the devs have been very careful about which methods to include and which methods not to include in the classes. It makes the Path class less attractive that it could be, at the same time, this care is understandable. The Path class will probably slowly add important features.

I try to use pathlib as often as I can because of this past experience with other Path classes which saved me a lot of pain in my programs. I think yes, pathlib can completely replace os.path, and using os.path has a taste of backward compatibility!
Reply
#3
Path (of pathlib) instantiates a concrete path for the platform the code is running on.
This means both purepaths (purely computational operations) and Concrete ( which inherit from pure paths but also provide I/O operations)

I have been using pathlib almost exclusively for about two months now, and really like how clean it is.

Since it is 100% object oriented, you can do things like:
from pathlib import Path
home = Path('.')
datapath = home / 'data'
jsonpath = datapath / 'json'
>>> imagepath = home / '..' / 'images'
>>> image_list = [x for x in imagepath.iterdir()]
>>> for image in image_list:
...     print(image)
...
..\images\advancedsplash.png
..\images\AG00028_.gif
..\images\AG00039_.gif
..\images\AG00178_.gif
..\images\AG00183_.gif
..\images\AG00185_.gif
..\images\aquabutton.png
..\images\aquachecked.ico
..\images\aquaflagged.ico
..\images\aquanotchecked.ico
>>>>>> imagepath.resolve()
WindowsPath('Z:/python/q-t/r/RfcViewerJupyterVenv/RfcViewerJupyter/images')
>>>
Reply
#4
Thank you both for answers! I can confidently walk around os.path in favour of pathlib from now :)
Reply
#5
I only want to add that there is an issue (or bug) with pathlib: if you try to subclass directly pathlib.Path in order to add your own custom methods, it FAILS!

A simple solution seems to subclass the concrete class type(pathlib.Path()) instead.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  String replacement in DB WJSwan 0 712 Dec-28-2022, 05:31 AM
Last Post: WJSwan
Exclamation IndexError: Replacement index 2 out of range for positional args tuple - help? MrKnd94 2 5,967 Oct-14-2022, 09:57 PM
Last Post: MrKnd94
  Pathlib import not working chriswrcg 9 3,519 May-29-2022, 07:37 PM
Last Post: snippsat
  WebDriverException: Message: 'PATH TO CHROME DRIVER' executable needs to be in PATH Led_Zeppelin 1 2,149 Sep-09-2021, 01:25 PM
Last Post: Yoriz
  deleting an empty Dir, using pathlib.Path tester_V 9 5,671 Jul-01-2021, 01:53 PM
Last Post: Gribouillis
  Trying to pathlib instead of os.path tester_V 4 2,430 Jun-22-2021, 04:15 AM
Last Post: tester_V
  pathlib destpath.exists() true even file does not exist NaN 9 4,565 Dec-01-2020, 12:43 PM
Last Post: NaN
  Extract the largest value from a group without replacement (beginner) preliator 1 2,039 Aug-12-2020, 01:56 PM
Last Post: DPaul
  Simple automated SoapAPI Call with single variable replacement from csv asaxty 1 2,056 Jun-30-2020, 06:38 PM
Last Post: asaxty
  Question about if with () or without () / pathlib Tecuma 3 2,157 Apr-02-2020, 10:02 AM
Last Post: Tecuma

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020