Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to find target path of link if the file is a link file

Tags:

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)

like image 432
duhhunjonn Avatar asked Jul 09 '10 12:07

duhhunjonn


People also ask

How to find file from symbolic link?

To find which files link symbolically to a particular file, you can use the find command and the -lname option with a file name, as Listing 13 illustrates.

How do you check if a file is a link in Python?

islink() method in Python is used to check whether the given path represents an existing directory entry that is a symbolic link or not. Note: If symbolic links are not supported by the Python runtime then os. path. islink() method always returns False.

How do you check if a file is a symlink in C?

S_ISLNK() macro can be used for to test whether a directory is symbolic link or it can be used to check only whether a file is symbolic link or not.

What is OS path Realpath?

os.path. realpath (path, *, strict=False) Return the canonical path of the specified filename, eliminating any symbolic links encountered in the path (if they are supported by the operating system). If a path doesn't exist or a symlink loop is encountered, and strict is True , OSError is raised.


1 Answers

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.

like image 119
Matthew Flaschen Avatar answered Oct 14 '22 16:10

Matthew Flaschen