By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,908 Members | 1,860 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,908 IT Pros & Developers. It's quick & easy.

Walking The Right Path

P: n/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?
TIA,
----------------------------------------------------------------------------
Tim Daneliuk tu****@tundraware.com
PGP Key: http://www.tundraware.com/PGP/
Jan 8 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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')

Jan 8 '07 #2

P: n/a
Jakub Stolarski wrote:
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')
Yup, that does just what I want. By why, pray tell, do you consider it
ugly ...?
--
----------------------------------------------------------------------------
Tim Daneliuk tu****@tundraware.com
PGP Key: http://www.tundraware.com/PGP/
Jan 8 '07 #3

P: n/a
Tim Daneliuk wrote:
Jakub Stolarski wrote:
>Tim Daneliuk napisal(a):
>>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')

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...
--
----------------------------------------------------------------------------
Tim Daneliuk tu****@tundraware.com
PGP Key: http://www.tundraware.com/PGP/
Jan 8 '07 #4

P: n/a
Tim Daneliuk wrote:
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
Jan 8 '07 #5

P: n/a
Peter Otten wrote:
Tim Daneliuk wrote:
>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
Thanks - I couldn't find it because it was right in front of my nose :)
--
----------------------------------------------------------------------------
Tim Daneliuk tu****@tundraware.com
PGP Key: http://www.tundraware.com/PGP/
Jan 8 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.