Walking The Right Path

T

Tim Daneliuk

Ah yes, moral philosophy and python all come together... Er, that is to day:

Imagine you have this situation on a *nix filesystem:

Symlink A: /foo -> /usr/home
Symlink B: /bar -> /foo/username


If I do this:

import os
print os.path.realpath("/bar")

I get this (as one would expect):

/usr/home/username

However, what if I want to get back the result in this form:

/foo/username


IOW, is there a way to return a symlink-based path which contains
the symlink pointer as is was *defined* not as it expands?


TIA,
 
J

Jakub Stolarski

Tim Daneliuk napisal(a):
Ah yes, moral philosophy and python all come together... Er, that is to day:

Imagine you have this situation on a *nix filesystem:

Symlink A: /foo -> /usr/home
Symlink B: /bar -> /foo/username


If I do this:

import os
print os.path.realpath("/bar")

I get this (as one would expect):

/usr/home/username

However, what if I want to get back the result in this form:

/foo/username


IOW, is there a way to return a symlink-based path which contains
the symlink pointer as is was *defined* not as it expands?

One way (but very ugly):
print os.path._resolve_link('/bar')
 
T

Tim Daneliuk

Jakub said:
Tim Daneliuk napisal(a):

One way (but very ugly):
print os.path._resolve_link('/bar')

Yup, that does just what I want. By why, pray tell, do you consider it
ugly ...?
 
T

Tim Daneliuk

Tim said:
Yup, that does just what I want. By why, pray tell, do you consider it
ugly ...?

Whoops - that doesn't do it either. If I have this:

/foo -> /usr/bar
/usr/bar -> /usr1/bar

Then realpath or _resolve_link both return "/usr1/bar" when given "/foo"
(when what I want is "/usr/bar"). There is obviously some underlying
OS support to do this (in this case FreeBSD 4.x) because 'ls' shows
things as I prefer them - i.e. Links are shown as assigned not how they
actually resolve...
 
P

Peter Otten

Tim said:
IOW, is there a way to return a symlink-based path which contains
the symlink pointer as is was *defined* not as it expands?

os.readlink()

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top