Pathlib context (symlinks, junctions, case folding)

Hello all,

this is an idea that’s been rattling around my head for a few weeks on and off, and I freely admit I’m putting it here to get it out of my head, rather than seriously thinking this is a good idea.

The idea is to add context to pathlib objects, things like follow_symlink, follow_junction, maybe fold_case and all the other weird filesystem things that I don’t even know about.

p = pathlib.Path("/one/two/three", follow_symlinks=True)

from this point onwards, operations on p will gain the context of follow_symlinks=True, it would not have to be added to higher level operations, and such context could be passed through APIs that take Path objects.

r = p / "four"

would gain the context of follow_symlinks=True, any low level operation that changes based on the context could check it and do so.

The hope is we could stop adding follow_symlink/junction flags everywhere, still have the functionality when required, and make such options opaque to APIs that just want to think about paths.

thank you for your time,


It’s an interesting idea, and if you wanted to prototype it, I think you could:

  • Subclass pathlib.Path
  • Add a custom __init__() method to accept your new keyword arguments, and store them as instance variables
  • Add a custom with_segments() method that reads the instance variables and feeds them to new paths as keyword arguments. This propagates settings when you generate new paths via path.parent, path.iterdir() etc.
  • Add versions of stat() etc that read from the instance variables for defaults

This requires Python 3.12 for subclassing Path and the with_segments() method.

1 Like

Okey Dokey, I’ll give it a stab, nothing will be done quickly however :slight_smile:

It’s lovely to see the work to make Path extensible available.