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?
#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


Messages In This Thread
RE: Is pathlib a viable replacement for os.path? - by Larz60+ - Jan-12-2018, 10:38 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  replacement for fpformat.extract GbSig1998 4 319 Apr-12-2024, 06:15 PM
Last Post: deanhystad
  String replacement in DB WJSwan 0 774 Dec-28-2022, 05:31 AM
Last Post: WJSwan
Exclamation IndexError: Replacement index 2 out of range for positional args tuple - help? MrKnd94 2 6,584 Oct-14-2022, 09:57 PM
Last Post: MrKnd94
  Pathlib import not working chriswrcg 9 3,831 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,246 Sep-09-2021, 01:25 PM
Last Post: Yoriz
  deleting an empty Dir, using pathlib.Path tester_V 9 5,949 Jul-01-2021, 01:53 PM
Last Post: Gribouillis
  Trying to pathlib instead of os.path tester_V 4 2,540 Jun-22-2021, 04:15 AM
Last Post: tester_V
  pathlib destpath.exists() true even file does not exist NaN 9 4,750 Dec-01-2020, 12:43 PM
Last Post: NaN
  Extract the largest value from a group without replacement (beginner) preliator 1 2,112 Aug-12-2020, 01:56 PM
Last Post: DPaul
  Simple automated SoapAPI Call with single variable replacement from csv asaxty 1 2,139 Jun-30-2020, 06:38 PM
Last Post: asaxty

Forum Jump:

User Panel Messages

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