Why would one use both, os.path.abspath and os.path.realpath?

前端 未结 3 1512
忘掉有多难
忘掉有多难 2020-12-02 06:21

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.

Howeve

3条回答
  •  心在旅途
    2020-12-02 07:12

    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"
    

提交回复
热议问题