serhii.net

In the middle of the desert you can say anything you want

23 Sep 2022

Python path .resolve() doesn't expand ~, only .. and symlinks!

I religiously do .realpath() pretty much every time I get a path from user input. Naively believing it also expands ~ etc.

Once I forgot and once I entered a non-expanded path myself: ~/this/

Then was tracking it as a bug, and found this bundle of joy:

/home/sh/me/dir~/me/dir/Checkpoints/checkpoint_288

It is in fact not illegal to create a directory called ~ in Unix.

And the things that used it as-is where there, and the things that were using it after a realpath were using another directory.

OK, I resolve()-d it - still the same.

TIL Path.resolve() takes care of symlinks and ..-like components, but not ~. So it should be Path.expanduser().resolve() from now on.

Nel mezzo del deserto posso dire tutto quello che voglio.