471,348 Members | 1,877 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,348 software developers and data experts.

os.readlink returning value

I was reading os.readlink doc which says:

readlink( path)

Return a string representing the path to which the symbolic link
points. The result may be either an absolute or relative pathname; if
it is relative, it may be converted to an absolute pathname using
os.path.join(os.path.dirname(path), result). Availability: Macintosh,
Unix.

....It's not clear to me when the returning result could be absolute
and when it could be relative.
Could someone clarify this point?

Nov 2 '07 #1
4 6822
En Thu, 01 Nov 2007 22:51:14 -0300, Giampaolo Rodola' <gn****@gmail.com>
escribió:
I was reading os.readlink doc which says:

readlink( path)

Return a string representing the path to which the symbolic link
points. The result may be either an absolute or relative pathname; if
it is relative, it may be converted to an absolute pathname using
os.path.join(os.path.dirname(path), result). Availability: Macintosh,
Unix.

...It's not clear to me when the returning result could be absolute
and when it could be relative.
Could someone clarify this point?
That depends on how the symlink was created. Assume the current directory
is /usr/home/giampaolo/any/dir
ln -s ../foo/bar
creates a symbolic link at /usr/home/giampaolo/any/dir/bar pointing to
.../foo/bar (relative to where the link resides). That is, actually
pointing to /usr/home/giampaolo/any/foo/bar (but this absolute path is NOT
stored on the link itself - only ../foo/bar)

Now, a program whose current directory is /usr/home/giampaolo executes
this:
readlink("any/dir/bar")
It will return the string "../foo/bar". One must resolve the .. reference
relative to where the link resides, NOT relative to the current directory.
That is, relative to any/dir. os.path.dirname("any/dir/bar") returns
exactly that. Then, the suggested expression in the docs evaluates to
"any/dir/../foo/bar" - it's not an absolute pathname yet, one should use
abspath() on it. Or instead
os.path.join(os.path.abspath(os.path.dirname(path) ), result)

--
Gabriel Genellina

Nov 2 '07 #2
Giampaolo Rodola' <gn****@gmail.comwrote:
>
I was reading os.readlink doc which says:

readlink( path)

Return a string representing the path to which the symbolic link
points. The result may be either an absolute or relative pathname; if
it is relative, it may be converted to an absolute pathname using
os.path.join(os.path.dirname(path), result). Availability: Macintosh,
Unix.

...It's not clear to me when the returning result could be absolute
and when it could be relative.
Could someone clarify this point?
It will be relative if the symbolic link is relative, and absolute if the
symbolic link is absolute.

ln -s ../../over/there here1
ln -s /home/timr/spot here2

"here1" is a relative link. "here2" is an absolute link.
--
Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Nov 2 '07 #3
On 2 Nov, 05:30, "Gabriel Genellina" <gagsl-...@yahoo.com.arwrote:
En Thu, 01 Nov 2007 22:51:14 -0300, Giampaolo Rodola' <gne...@gmail.com
escribió:
I was reading os.readlink doc which says:
readlink( path)
Return a string representing the path to which the symbolic link
points. The result may be either an absolute or relative pathname; if
it is relative, it may be converted to an absolute pathname using
os.path.join(os.path.dirname(path), result). Availability: Macintosh,
Unix.
...It's not clear to me when the returning result could be absolute
and when it could be relative.
Could someone clarify this point?

That depends on how the symlink was created. Assume the current directory
is /usr/home/giampaolo/any/dirln -s ../foo/bar

creates a symbolic link at /usr/home/giampaolo/any/dir/bar pointing to
../foo/bar (relative to where the link resides). That is, actually
pointing to /usr/home/giampaolo/any/foo/bar (but this absolute path is NOT
stored on the link itself - only ../foo/bar)

Now, a program whose current directory is /usr/home/giampaolo executes
this:
readlink("any/dir/bar")
It will return the string "../foo/bar". One must resolve the .. reference
relative to where the link resides, NOT relative to the current directory..
That is, relative to any/dir. os.path.dirname("any/dir/bar") returns
exactly that. Then, the suggested expression in the docs evaluates to
"any/dir/../foo/bar" - it's not an absolute pathname yet, one should use
abspath() on it. Or instead
os.path.join(os.path.abspath(os.path.dirname(path) ), result)

--
Gabriel Genellina
Thanks for the examplanation.
I'd only want to do the same as what ls does in such cases.
Imho, os.readlink() should have the same behaviour of ls, isn't it?

Nov 2 '07 #4
En Fri, 02 Nov 2007 13:44:37 -0300, Giampaolo Rodola' <gn****@gmail.com>
escribió:
On 2 Nov, 05:30, "Gabriel Genellina" <gagsl-...@yahoo.com.arwrote:
>En Thu, 01 Nov 2007 22:51:14 -0300, Giampaolo Rodola' <gne...@gmail.com>
escribió:
readlink( path)
...It's not clear to me when the returning result could be absolute
and when it could be relative.

That depends on how the symlink was created. Assume the current
I'd only want to do the same as what ls does in such cases.
Imho, os.readlink() should have the same behaviour of ls, isn't it?
Yes; os.readlink shows the same as ls -l

--
Gabriel Genellina

Nov 3 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Mountain Man | last post: by
9 posts views Thread by mjm | last post: by
11 posts views Thread by JKop | last post: by
9 posts views Thread by Thomas Mlynarczyk | last post: by
4 posts views Thread by scparker | last post: by
8 posts views Thread by Michal Nazarewicz | last post: by
2 posts views Thread by JBW | last post: by
1 post views Thread by Ronak mishra | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.