9

There are many examples of resolving the path of a file while following symlinks, and I'd like to get the absolute path to a file without resolving symlinks. I think this is the solutions:

 ex=Path('example.txt') 
 abs_ex = ex.parent.resolve()/ex

Is there something I'm missing in terms of another function or argument to resolve()?

There is an answer regarding using os.path.abspath but I'm asking about pathlib in Python 3.6 or better.

Arthur Ulfeldt
  • 90,827
  • 27
  • 201
  • 284
Ray Salemi
  • 5,247
  • 4
  • 30
  • 63
  • could you elaborate on the not resolving symlinks part. A detailed answer to that would depend on OS an possibly the specific filesystem in use. – Arthur Ulfeldt Mar 28 '18 at 20:38
  • If this question is really about symlinks then it's not duplicate of the one above. If it's not really about symlinks then the link in the comments here would cover it. – Arthur Ulfeldt Mar 28 '18 at 20:39
  • 3
    As tagged this question is about the pathlib and is not a duplicate of the os.path.abspath() answer. – Ray Salemi Mar 28 '18 at 22:06
  • Sorry this question got marked dupe, I agree that it's not and put in into the re-open queue for re-review. thanks for your patience Ray :-) – Arthur Ulfeldt Apr 02 '18 at 18:39
  • @ArthurUlfeldt Did they decide this is a dup? – Ray Salemi Mar 06 '19 at 19:15
  • The answer this is marked as a "dupe" of is about an operating system that doesn't even have the feature this question is about, namely symlinks. It was marked as a dupe and clearly is not. I'll nominate it for reopening and i edited it to put the differentes in **bold** and state them twice. – Arthur Ulfeldt Mar 10 '19 at 02:13
  • 2
    @ArthurUlfeldt: The dupe target is OS-agnostic. The example merely happens to be a Windows path. The answer in the dupe target does not resolve symlinks, regardless of OS. (Also, Windows has symlinks.) – user2357112 Mar 10 '19 at 02:22
  • Regarding the pathlib aspect, the closest pathlib comes to supporting this is the [undocumented, untested, needs-fixing, considered-for-deprecation `Path.absolute()` method](https://bugs.python.org/issue29688). Relying on this method is of course a terrible idea. `os.path.abspath` is a much better option. – user2357112 Mar 10 '19 at 02:47

0 Answers0