For convenience in usage, path names may include relative or symbolic components.  For example in a relative path, a single period (.) may be used as an alias for the current directory, two periods (..)  for the parent of the current directory, a tilde character(~) indicates the user's home directory, that is just to mention a few.

The realpath() function in  the path sub-module of the  os module is used to obtain the canonical path of a given pathname The canonical path is the absolute path that has all symbolic links and redundant components resolved to an absolute pathname.

To use the realpath() function, we will first need to import it in our program as follows.

from os.path import realpath
p A path that may contain symbolic components.

The function resolves all the symbolic components present in the path and returns the absolute path of the input path.  It does not validate whether the path actually exist or it doesn't.

from os.path import realpath

p = r"images\john.png"


In the above example , the interpreter is called at desktop folder, the relative path gets resolved to the absolute path.