Why would one use both, os.path.abspath and os.path.realpath?
up vote
45
down vote
favorite
In multiple open source projects, I have seen people do os.path.abspath(os.path.realpath(__file__))
to get the absolute path to the current file.
However, I find that os.path.abspath(__file__)
and os.path.realpath(__file__)
produce the same result. os.path.abspath(os.path.realpath(__file__))
seems to be a bit redundant.
Is there a reason people are using that?
python
add a comment |
up vote
45
down vote
favorite
In multiple open source projects, I have seen people do os.path.abspath(os.path.realpath(__file__))
to get the absolute path to the current file.
However, I find that os.path.abspath(__file__)
and os.path.realpath(__file__)
produce the same result. os.path.abspath(os.path.realpath(__file__))
seems to be a bit redundant.
Is there a reason people are using that?
python
add a comment |
up vote
45
down vote
favorite
up vote
45
down vote
favorite
In multiple open source projects, I have seen people do os.path.abspath(os.path.realpath(__file__))
to get the absolute path to the current file.
However, I find that os.path.abspath(__file__)
and os.path.realpath(__file__)
produce the same result. os.path.abspath(os.path.realpath(__file__))
seems to be a bit redundant.
Is there a reason people are using that?
python
In multiple open source projects, I have seen people do os.path.abspath(os.path.realpath(__file__))
to get the absolute path to the current file.
However, I find that os.path.abspath(__file__)
and os.path.realpath(__file__)
produce the same result. os.path.abspath(os.path.realpath(__file__))
seems to be a bit redundant.
Is there a reason people are using that?
python
python
edited Jun 1 at 6:48
Trilarion
6,47153875
6,47153875
asked Jun 16 '16 at 15:30
Chonghao Huang
1,03641319
1,03641319
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
up vote
38
down vote
accepted
os.path.realpath
derefences symbolic links on those operating systems which support them.
os.path.abspath
simply removes things like .
and ..
from the path giving a full path from the root of the directory tree to the named file (or symlink)
For example, on Ubuntu
$ ls -l
total 0
-rw-rw-r-- 1 guest guest 0 Jun 16 08:36 a
lrwxrwxrwx 1 guest guest 1 Jun 16 08:36 b -> a
$ python
Python 2.7.11 (default, Dec 15 2015, 16:46:19)
[GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from os.path import abspath, realpath
>>> abspath('b')
'/home/guest/play/paths/b'
>>> realpath('b')
'/home/guest/play/paths/a'
Symlinks can contain relative paths, hence the need to use both. The inner call to realpath
might return a path with embedded ..
parts, which abspath
then removes.
4
While this answer describes the difference between the two functions, it isn't true that the result ofrealpath()
can contain..
components and doesn't really answer the question of why you would use both; jobrad's answer is more accurate.
– Miles
Mar 24 '17 at 3:41
add a comment |
up vote
66
down vote
For your stated scenario, there is no reason to combine realpath and abspath, since os.path.realpath
actually calls os.path.abspath
before returning a result (I checked Python 2.5 to Python 3.6).
os.path.abspath
returns the absolute path, but does NOT resolve symlinks.os.path.realpath
will first resolve any symbolic links in the path, and then return the absolute path.
However, if you expect your path to contain a ~
, neither abspath or realpath will resolve ~
to the user's home directory, and the resulting path will be invalid. You will need to use os.path.expanduser
to resolve this to the user's directory.
For the sake of a thorough explanation, here are some results which I've verified in Windows and Linux, in Python 3.4 and Python 2.6. The current directory (./
) is my home directory, which looks like this:
myhome
|- data (symlink to /mnt/data)
|- subdir (extra directory, for verbose explanation)
# os.path.abspath returns the absolute path, but does NOT resolve symlinks
os.path.abspath('./')
'/home/myhome'
os.path.abspath('./subdir/../data')
'/home/myhome/data'
# os.path.realpath will resolve symlinks AND return an absolute path from a relative path
os.path.realpath('./')
'/home/myhome'
os.path.realpath('./subdir/../')
'/home/myhome'
os.path.realpath('./subdir/../data')
'/mnt/data'
# NEITHER abspath or realpath will resolve or remove ~.
os.path.abspath('~/data')
'/home/myhome/~/data'
os.path.realpath('~/data')
'/home/myhome/~/data'
# And the returned path will be invalid
os.path.exists(os.path.abspath('~/data'))
False
os.path.exists(os.path.realpath('~/data'))
False
# Use realpath + expanduser to resolve ~
os.path.realpath(os.path.expanduser('./subdir/../data'))
'/mnt/data'
2
Nice. To make your point your last example should beos.path.realpath(os.path.expanduser('~/subdir/../data'))
– Arthur
Apr 7 at 16:13
Then also do not forget about os.path.expandvars if variables are used in a path
– Slavenskij
Apr 11 at 6:55
add a comment |
up vote
3
down vote
In the layman terms, if you are trying to get the path of a shortcut file, absolute path gives the complete path of the file present in the shortcut location, while realpath gives the original location path of the file.
Absolute path, os.path.abspath(), gives the complete path of the file which is located in the current working directory or the directory you mentioned.
Real path, os.path.realpath(), gives the complete path of the file which is being referred.
Eg:
file = "shortcut_folder/filename"
os.path.abspath(file) = "C:/Desktop/shortcut_folder/filename"
os.path.realpath(file) = "D:/PyCharmProjects/Python1stClass/filename"
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
38
down vote
accepted
os.path.realpath
derefences symbolic links on those operating systems which support them.
os.path.abspath
simply removes things like .
and ..
from the path giving a full path from the root of the directory tree to the named file (or symlink)
For example, on Ubuntu
$ ls -l
total 0
-rw-rw-r-- 1 guest guest 0 Jun 16 08:36 a
lrwxrwxrwx 1 guest guest 1 Jun 16 08:36 b -> a
$ python
Python 2.7.11 (default, Dec 15 2015, 16:46:19)
[GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from os.path import abspath, realpath
>>> abspath('b')
'/home/guest/play/paths/b'
>>> realpath('b')
'/home/guest/play/paths/a'
Symlinks can contain relative paths, hence the need to use both. The inner call to realpath
might return a path with embedded ..
parts, which abspath
then removes.
4
While this answer describes the difference between the two functions, it isn't true that the result ofrealpath()
can contain..
components and doesn't really answer the question of why you would use both; jobrad's answer is more accurate.
– Miles
Mar 24 '17 at 3:41
add a comment |
up vote
38
down vote
accepted
os.path.realpath
derefences symbolic links on those operating systems which support them.
os.path.abspath
simply removes things like .
and ..
from the path giving a full path from the root of the directory tree to the named file (or symlink)
For example, on Ubuntu
$ ls -l
total 0
-rw-rw-r-- 1 guest guest 0 Jun 16 08:36 a
lrwxrwxrwx 1 guest guest 1 Jun 16 08:36 b -> a
$ python
Python 2.7.11 (default, Dec 15 2015, 16:46:19)
[GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from os.path import abspath, realpath
>>> abspath('b')
'/home/guest/play/paths/b'
>>> realpath('b')
'/home/guest/play/paths/a'
Symlinks can contain relative paths, hence the need to use both. The inner call to realpath
might return a path with embedded ..
parts, which abspath
then removes.
4
While this answer describes the difference between the two functions, it isn't true that the result ofrealpath()
can contain..
components and doesn't really answer the question of why you would use both; jobrad's answer is more accurate.
– Miles
Mar 24 '17 at 3:41
add a comment |
up vote
38
down vote
accepted
up vote
38
down vote
accepted
os.path.realpath
derefences symbolic links on those operating systems which support them.
os.path.abspath
simply removes things like .
and ..
from the path giving a full path from the root of the directory tree to the named file (or symlink)
For example, on Ubuntu
$ ls -l
total 0
-rw-rw-r-- 1 guest guest 0 Jun 16 08:36 a
lrwxrwxrwx 1 guest guest 1 Jun 16 08:36 b -> a
$ python
Python 2.7.11 (default, Dec 15 2015, 16:46:19)
[GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from os.path import abspath, realpath
>>> abspath('b')
'/home/guest/play/paths/b'
>>> realpath('b')
'/home/guest/play/paths/a'
Symlinks can contain relative paths, hence the need to use both. The inner call to realpath
might return a path with embedded ..
parts, which abspath
then removes.
os.path.realpath
derefences symbolic links on those operating systems which support them.
os.path.abspath
simply removes things like .
and ..
from the path giving a full path from the root of the directory tree to the named file (or symlink)
For example, on Ubuntu
$ ls -l
total 0
-rw-rw-r-- 1 guest guest 0 Jun 16 08:36 a
lrwxrwxrwx 1 guest guest 1 Jun 16 08:36 b -> a
$ python
Python 2.7.11 (default, Dec 15 2015, 16:46:19)
[GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from os.path import abspath, realpath
>>> abspath('b')
'/home/guest/play/paths/b'
>>> realpath('b')
'/home/guest/play/paths/a'
Symlinks can contain relative paths, hence the need to use both. The inner call to realpath
might return a path with embedded ..
parts, which abspath
then removes.
edited Mar 24 '17 at 4:59
Martin Tournoij
17.6k126191
17.6k126191
answered Jun 16 '16 at 15:36
kdopen
6,53772939
6,53772939
4
While this answer describes the difference between the two functions, it isn't true that the result ofrealpath()
can contain..
components and doesn't really answer the question of why you would use both; jobrad's answer is more accurate.
– Miles
Mar 24 '17 at 3:41
add a comment |
4
While this answer describes the difference between the two functions, it isn't true that the result ofrealpath()
can contain..
components and doesn't really answer the question of why you would use both; jobrad's answer is more accurate.
– Miles
Mar 24 '17 at 3:41
4
4
While this answer describes the difference between the two functions, it isn't true that the result of
realpath()
can contain ..
components and doesn't really answer the question of why you would use both; jobrad's answer is more accurate.– Miles
Mar 24 '17 at 3:41
While this answer describes the difference between the two functions, it isn't true that the result of
realpath()
can contain ..
components and doesn't really answer the question of why you would use both; jobrad's answer is more accurate.– Miles
Mar 24 '17 at 3:41
add a comment |
up vote
66
down vote
For your stated scenario, there is no reason to combine realpath and abspath, since os.path.realpath
actually calls os.path.abspath
before returning a result (I checked Python 2.5 to Python 3.6).
os.path.abspath
returns the absolute path, but does NOT resolve symlinks.os.path.realpath
will first resolve any symbolic links in the path, and then return the absolute path.
However, if you expect your path to contain a ~
, neither abspath or realpath will resolve ~
to the user's home directory, and the resulting path will be invalid. You will need to use os.path.expanduser
to resolve this to the user's directory.
For the sake of a thorough explanation, here are some results which I've verified in Windows and Linux, in Python 3.4 and Python 2.6. The current directory (./
) is my home directory, which looks like this:
myhome
|- data (symlink to /mnt/data)
|- subdir (extra directory, for verbose explanation)
# os.path.abspath returns the absolute path, but does NOT resolve symlinks
os.path.abspath('./')
'/home/myhome'
os.path.abspath('./subdir/../data')
'/home/myhome/data'
# os.path.realpath will resolve symlinks AND return an absolute path from a relative path
os.path.realpath('./')
'/home/myhome'
os.path.realpath('./subdir/../')
'/home/myhome'
os.path.realpath('./subdir/../data')
'/mnt/data'
# NEITHER abspath or realpath will resolve or remove ~.
os.path.abspath('~/data')
'/home/myhome/~/data'
os.path.realpath('~/data')
'/home/myhome/~/data'
# And the returned path will be invalid
os.path.exists(os.path.abspath('~/data'))
False
os.path.exists(os.path.realpath('~/data'))
False
# Use realpath + expanduser to resolve ~
os.path.realpath(os.path.expanduser('./subdir/../data'))
'/mnt/data'
2
Nice. To make your point your last example should beos.path.realpath(os.path.expanduser('~/subdir/../data'))
– Arthur
Apr 7 at 16:13
Then also do not forget about os.path.expandvars if variables are used in a path
– Slavenskij
Apr 11 at 6:55
add a comment |
up vote
66
down vote
For your stated scenario, there is no reason to combine realpath and abspath, since os.path.realpath
actually calls os.path.abspath
before returning a result (I checked Python 2.5 to Python 3.6).
os.path.abspath
returns the absolute path, but does NOT resolve symlinks.os.path.realpath
will first resolve any symbolic links in the path, and then return the absolute path.
However, if you expect your path to contain a ~
, neither abspath or realpath will resolve ~
to the user's home directory, and the resulting path will be invalid. You will need to use os.path.expanduser
to resolve this to the user's directory.
For the sake of a thorough explanation, here are some results which I've verified in Windows and Linux, in Python 3.4 and Python 2.6. The current directory (./
) is my home directory, which looks like this:
myhome
|- data (symlink to /mnt/data)
|- subdir (extra directory, for verbose explanation)
# os.path.abspath returns the absolute path, but does NOT resolve symlinks
os.path.abspath('./')
'/home/myhome'
os.path.abspath('./subdir/../data')
'/home/myhome/data'
# os.path.realpath will resolve symlinks AND return an absolute path from a relative path
os.path.realpath('./')
'/home/myhome'
os.path.realpath('./subdir/../')
'/home/myhome'
os.path.realpath('./subdir/../data')
'/mnt/data'
# NEITHER abspath or realpath will resolve or remove ~.
os.path.abspath('~/data')
'/home/myhome/~/data'
os.path.realpath('~/data')
'/home/myhome/~/data'
# And the returned path will be invalid
os.path.exists(os.path.abspath('~/data'))
False
os.path.exists(os.path.realpath('~/data'))
False
# Use realpath + expanduser to resolve ~
os.path.realpath(os.path.expanduser('./subdir/../data'))
'/mnt/data'
2
Nice. To make your point your last example should beos.path.realpath(os.path.expanduser('~/subdir/../data'))
– Arthur
Apr 7 at 16:13
Then also do not forget about os.path.expandvars if variables are used in a path
– Slavenskij
Apr 11 at 6:55
add a comment |
up vote
66
down vote
up vote
66
down vote
For your stated scenario, there is no reason to combine realpath and abspath, since os.path.realpath
actually calls os.path.abspath
before returning a result (I checked Python 2.5 to Python 3.6).
os.path.abspath
returns the absolute path, but does NOT resolve symlinks.os.path.realpath
will first resolve any symbolic links in the path, and then return the absolute path.
However, if you expect your path to contain a ~
, neither abspath or realpath will resolve ~
to the user's home directory, and the resulting path will be invalid. You will need to use os.path.expanduser
to resolve this to the user's directory.
For the sake of a thorough explanation, here are some results which I've verified in Windows and Linux, in Python 3.4 and Python 2.6. The current directory (./
) is my home directory, which looks like this:
myhome
|- data (symlink to /mnt/data)
|- subdir (extra directory, for verbose explanation)
# os.path.abspath returns the absolute path, but does NOT resolve symlinks
os.path.abspath('./')
'/home/myhome'
os.path.abspath('./subdir/../data')
'/home/myhome/data'
# os.path.realpath will resolve symlinks AND return an absolute path from a relative path
os.path.realpath('./')
'/home/myhome'
os.path.realpath('./subdir/../')
'/home/myhome'
os.path.realpath('./subdir/../data')
'/mnt/data'
# NEITHER abspath or realpath will resolve or remove ~.
os.path.abspath('~/data')
'/home/myhome/~/data'
os.path.realpath('~/data')
'/home/myhome/~/data'
# And the returned path will be invalid
os.path.exists(os.path.abspath('~/data'))
False
os.path.exists(os.path.realpath('~/data'))
False
# Use realpath + expanduser to resolve ~
os.path.realpath(os.path.expanduser('./subdir/../data'))
'/mnt/data'
For your stated scenario, there is no reason to combine realpath and abspath, since os.path.realpath
actually calls os.path.abspath
before returning a result (I checked Python 2.5 to Python 3.6).
os.path.abspath
returns the absolute path, but does NOT resolve symlinks.os.path.realpath
will first resolve any symbolic links in the path, and then return the absolute path.
However, if you expect your path to contain a ~
, neither abspath or realpath will resolve ~
to the user's home directory, and the resulting path will be invalid. You will need to use os.path.expanduser
to resolve this to the user's directory.
For the sake of a thorough explanation, here are some results which I've verified in Windows and Linux, in Python 3.4 and Python 2.6. The current directory (./
) is my home directory, which looks like this:
myhome
|- data (symlink to /mnt/data)
|- subdir (extra directory, for verbose explanation)
# os.path.abspath returns the absolute path, but does NOT resolve symlinks
os.path.abspath('./')
'/home/myhome'
os.path.abspath('./subdir/../data')
'/home/myhome/data'
# os.path.realpath will resolve symlinks AND return an absolute path from a relative path
os.path.realpath('./')
'/home/myhome'
os.path.realpath('./subdir/../')
'/home/myhome'
os.path.realpath('./subdir/../data')
'/mnt/data'
# NEITHER abspath or realpath will resolve or remove ~.
os.path.abspath('~/data')
'/home/myhome/~/data'
os.path.realpath('~/data')
'/home/myhome/~/data'
# And the returned path will be invalid
os.path.exists(os.path.abspath('~/data'))
False
os.path.exists(os.path.realpath('~/data'))
False
# Use realpath + expanduser to resolve ~
os.path.realpath(os.path.expanduser('./subdir/../data'))
'/mnt/data'
answered Oct 28 '16 at 18:29
jobrad
761154
761154
2
Nice. To make your point your last example should beos.path.realpath(os.path.expanduser('~/subdir/../data'))
– Arthur
Apr 7 at 16:13
Then also do not forget about os.path.expandvars if variables are used in a path
– Slavenskij
Apr 11 at 6:55
add a comment |
2
Nice. To make your point your last example should beos.path.realpath(os.path.expanduser('~/subdir/../data'))
– Arthur
Apr 7 at 16:13
Then also do not forget about os.path.expandvars if variables are used in a path
– Slavenskij
Apr 11 at 6:55
2
2
Nice. To make your point your last example should be
os.path.realpath(os.path.expanduser('~/subdir/../data'))
– Arthur
Apr 7 at 16:13
Nice. To make your point your last example should be
os.path.realpath(os.path.expanduser('~/subdir/../data'))
– Arthur
Apr 7 at 16:13
Then also do not forget about os.path.expandvars if variables are used in a path
– Slavenskij
Apr 11 at 6:55
Then also do not forget about os.path.expandvars if variables are used in a path
– Slavenskij
Apr 11 at 6:55
add a comment |
up vote
3
down vote
In the layman terms, if you are trying to get the path of a shortcut file, absolute path gives the complete path of the file present in the shortcut location, while realpath gives the original location path of the file.
Absolute path, os.path.abspath(), gives the complete path of the file which is located in the current working directory or the directory you mentioned.
Real path, os.path.realpath(), gives the complete path of the file which is being referred.
Eg:
file = "shortcut_folder/filename"
os.path.abspath(file) = "C:/Desktop/shortcut_folder/filename"
os.path.realpath(file) = "D:/PyCharmProjects/Python1stClass/filename"
add a comment |
up vote
3
down vote
In the layman terms, if you are trying to get the path of a shortcut file, absolute path gives the complete path of the file present in the shortcut location, while realpath gives the original location path of the file.
Absolute path, os.path.abspath(), gives the complete path of the file which is located in the current working directory or the directory you mentioned.
Real path, os.path.realpath(), gives the complete path of the file which is being referred.
Eg:
file = "shortcut_folder/filename"
os.path.abspath(file) = "C:/Desktop/shortcut_folder/filename"
os.path.realpath(file) = "D:/PyCharmProjects/Python1stClass/filename"
add a comment |
up vote
3
down vote
up vote
3
down vote
In the layman terms, if you are trying to get the path of a shortcut file, absolute path gives the complete path of the file present in the shortcut location, while realpath gives the original location path of the file.
Absolute path, os.path.abspath(), gives the complete path of the file which is located in the current working directory or the directory you mentioned.
Real path, os.path.realpath(), gives the complete path of the file which is being referred.
Eg:
file = "shortcut_folder/filename"
os.path.abspath(file) = "C:/Desktop/shortcut_folder/filename"
os.path.realpath(file) = "D:/PyCharmProjects/Python1stClass/filename"
In the layman terms, if you are trying to get the path of a shortcut file, absolute path gives the complete path of the file present in the shortcut location, while realpath gives the original location path of the file.
Absolute path, os.path.abspath(), gives the complete path of the file which is located in the current working directory or the directory you mentioned.
Real path, os.path.realpath(), gives the complete path of the file which is being referred.
Eg:
file = "shortcut_folder/filename"
os.path.abspath(file) = "C:/Desktop/shortcut_folder/filename"
os.path.realpath(file) = "D:/PyCharmProjects/Python1stClass/filename"
answered Oct 24 '17 at 14:06
Uday S
517312
517312
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f37863476%2fwhy-would-one-use-both-os-path-abspath-and-os-path-realpath%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown