python – symlink to another symlink without following the latter symlink

Question:

I want to create a symbolic link (A) to another symbolic link (B) which point to an actual file (C) on macOS with python >= 3.10:

A -> B -> C

I tried this:

from pathlib import Path

A = Path('/path/to/src')
B = '/path/to/another/symlink'

A.symlink_to(B)

print(A.resolve()) # prints C, not B

But it didn’t work.

How do I do this with python?

Asked By: Teddy C

||

Answers:

Use A.readlink() instead of A.resolve().

Answered By: Barmar
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.