classification
Title: Document various options for getting the absolute path from pathlib.Path objects
Type: enhancement Stage: needs patch
Components: Documentation Versions:
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: docs@python Nosy List: ChrisBarker, anowlcalledjosh, brett.cannon, docs@python, florisla, paul.moore, veky
Priority: normal Keywords:

Created on 2019-12-18 18:20 by brett.cannon, last changed 2020-02-25 07:44 by florisla.

Messages (10)
msg358638 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2019-12-18 18:20
The question on how best to get an absolute path from a pathlib.Path object keeps coming up (see https://bugs.python.org/issue29688, https://discuss.python.org/t/add-absolute-name-to-pathlib-path/2882/, and https://discuss.python.org/t/pathlib-absolute-vs-resolve/2573 as examples).

As pointed out across those posts, getting the absolute path is surprisingly subtle and varied depending on your needs. As such we should probably add a section somewhere in the pathlib docs explaining the various ways and why you would choose one over the other.
msg358854 - (view) Author: Chris Barker (ChrisBarker) * Date: 2019-12-24 18:17
Yes Please!

I'd offer to help, but I really don't get the intricacies involved. I will offer to proofread and copy-edit though, if that's helpful.

And I note that coincidentally, just in the last week, I needed to make an absolute path from a Path, and it took me far too long to figure out that .resolve() would do it for me. Then I needed to do it again three days later, and it again took a while -- "resolve" is simply not mnemonic for me, and I'm guessing a lot of people have the same issue.

And I didn't find .absolute(), cause it's not documented. I see in issue #29688 that there are reasons for that, but I'll make a plea:

Please document .absolute(), even if those docs say something like "may not work in all circumstances, not well tested". Alternatively, if it's decided that folks should just use .resolve() in all cases anyway, then make .absolute() an alias for .resolve(). 

Or if that's not a good option, then at least put some prominent notes in resolve() so people will find it.

Also -- I needed to read the resolve() docs carefully (and then test) to see if it was what I wanted - which I know, is what this issue is about.

In short -- I understand that this is a complex issue, but making an absolute path is a pretty common use case, and we've had os.path.abspath() for decades, so there should be one obvious way to do it, and it should be easily discoverable.

NOTE: even if there is no one to do the work of properly testing .absolute() at this point, it would b nice to at least decide now what the long term goal is -- will there be an absolute() or is resolve() all we really need?
msg361698 - (view) Author: Paul Moore (paul.moore) * (Python committer) Date: 2020-02-10 14:32
> In short -- I understand that this is a complex issue, but making an absolute path is a pretty common use case, and we've had os.path.abspath() for decades, so there should be one obvious way to do it, and it should be easily discoverable.

+1 on this.

Given that (as far as I can tell from the various discussions) `resolve` works fine as long as the file exists, maybe the key distinction to make is whether you have an existing file or not.

(More subtle questions like UNC path vs drive letter, mentioned on the Discourse thread, are probably things that we can defer to a "more advanced cases" discussion in the docs).
msg361877 - (view) Author: Floris Lambrechts (florisla) Date: 2020-02-12 11:33
I've written an "Absolute paths" section based on the knowledge I found in the various threads.

Any review is appreciated.

https://github.com/florisla/cpython/tree/pathlib-chapter-absolute-paths

With some related documentation changes:

https://github.com/florisla/cpython/tree/absolute-path-related-improvements
msg361878 - (view) Author: Paul Moore (paul.moore) * (Python committer) Date: 2020-02-12 11:38
You've provided links to your branches, but not to the specific text you're proposing to add. Can you link to a diff or something that shows what you've added more precisely?
msg361885 - (view) Author: Floris Lambrechts (florisla) Date: 2020-02-12 12:18
This is the new chapter:

https://github.com/florisla/cpython/commit/c146ad3d086fe9e401284c12fc670ea4f9398f3b
msg362030 - (view) Author: Vedran Čačić (veky) * Date: 2020-02-15 20:51
If we want something mnemonic, I'm sure nothing beats __abs__. (Hey, we have __truediv__ already!;)
msg362578 - (view) Author: Floris Lambrechts (florisla) Date: 2020-02-24 09:54
@ChrisBarker,

Could you review the proposed addition to the documentation?

https://github.com/florisla/cpython/commit/c146ad3d086fe9e401284c12fc670ea4f9398f3b
msg362579 - (view) Author: Floris Lambrechts (florisla) Date: 2020-02-24 09:55
(sorry, didn't see the GitHub comments before... I'll process those first.)
msg362627 - (view) Author: Floris Lambrechts (florisla) Date: 2020-02-25 07:44
Based on the feedback received in GitHub here:
https://github.com/florisla/cpython/commit/c146ad3d086fe9e401284c12fc670ea4f9398f3b

I made a new revision of the 'Absolute paths' chapter here:
https://github.com/florisla/cpython/blob/pathlib-chapter-absolute-paths-2/Doc/library/pathlib.rst#absolute-paths

Further feedback is welcome.

Changes:

* Be more 'in your face' about Path.resolve() being the recommended
  approach.
* Add separate section on Windows considerations
* Explain difference between Path.resolve() and os.path.isabs() w.r.t.
  checking for drive.
* Refer to 'mapped share' instead of 'mapped network share'.
* Explain replacement of substitute drive with final path.
* Mention os.path.abspath's upcasing of drive letter in case of
  a path missing a root.
* Mention different handling of junctions versus symlinks w.r.t.
  relative parts.

For brevity, I've kept the wording on substitute drive and handling of
junctions very short.

For the same reason I did not not include eryksun's (interesting!) info
on why mapped and substitute drives are non-canonical.

Not mentioning Path.resolve()'s behavior w.r.t. non-existing files since
that's documented in resolve() itself.
History
Date User Action Args
2020-02-25 07:44:09florislasetmessages: + msg362627
2020-02-24 09:55:54florislasetmessages: + msg362579
2020-02-24 09:54:56florislasetmessages: + msg362578
2020-02-15 20:51:59vekysetnosy: + veky
messages: + msg362030
2020-02-12 12:18:46florislasetmessages: + msg361885
2020-02-12 11:38:54paul.mooresetmessages: + msg361878
2020-02-12 11:33:07florislasetnosy: + florisla
messages: + msg361877
2020-02-10 14:32:10paul.mooresetnosy: + paul.moore
messages: + msg361698
2019-12-28 02:09:25anowlcalledjoshsetnosy: + anowlcalledjosh
2019-12-24 18:17:28ChrisBarkersetnosy: + ChrisBarker
messages: + msg358854
2019-12-18 18:20:50brett.cannoncreate