how to find if the file is a link file, and find the path of the target file (actual file pointed by the link file)
Asked
Active
Viewed 2.7k times
22
-
4how many times do you have to be given link to the same page? – SilentGhost Jul 09 '10 at 12:58
2 Answers
33
os.path.islink
(is it a link?) and os.path.realpath
(get ultimate pointed to path, regardless of whether it's a link).
If os.path.islink
is True, and you only want to follow the first link, use os.readlink
.

Matthew Flaschen
- 278,309
- 50
- 514
- 539
-
1os.path.realpath doesnot give the path of the actual target file's path as said. – duhhunjonn Jul 09 '10 at 13:22
-
2Looks like `os.path.realpath` does resolve symbolic links, including multiply-linked (multiply-nested?) symlinks. – dcrosta Jul 09 '10 at 14:47
-
1os.path.realpath does resolve the links only in Unix systems, not Windows. – Dmitry Petrov Apr 10 '17 at 22:12
-
`os.path.realpath` is not correctly resolving the link target in Python 2.3 on Linux – Jun 01 '18 at 14:57