Sibbs Gambling
Sibbs Gambling

Reputation: 20405

Get symlink "as is" in Python?

Give

$ ll ./
$ foo1
$ foo2 -> /absolute/path/to/bar2
$ foo3 -> bar3

I know

os.path.islink()

tells me if a file is a symlink.

I also know

os.path.realpath()

gives me the real, absolute path of a symlink. I have no problems with it returning me /absolute/path/to/bar2 for os.path.realpath('foo2').

But for os.path.realpath('foo3'), I want bar3 ("as is" in the title).

Is there a simpler way than computing relpath with foo3's directory as start?

Upvotes: 1

Views: 59

Answers (1)

0x5453
0x5453

Reputation: 13599

os.readlink appears to do what you want.

Upvotes: 1

Related Questions