명시된 시나리오의 경우 os.path.realpath실제로 os.path.abspath결과를 반환하기 전에 호출 하기 때문에 realpath와 abspath를 결합 할 이유가 없습니다 (Python 2.5에서 Python 3.6으로 확인).
os.path.abspath 절대 경로를 반환하지만 인수에서 심볼릭 링크를 확인하지 않습니다.
os.path.realpath 먼저 경로의 모든 심볼릭 링크를 확인한 다음 절대 경로를 반환합니다.
당신이 당신의 경로가 포함되어 기대하는 경우에는 ~, abspath 또는 realpath 어느 쪽도 해결할 수 ~사용자의 홈 디렉토리에 생성 된 경로가 잘못 될 것입니다 . os.path.expanduser이 문제를 사용자 디렉터리로 확인하려면를 사용해야 합니다.
철저한 설명을 위해 Windows 및 Linux, Python 3.4 및 Python 2.6에서 확인한 몇 가지 결과가 있습니다. 현재 디렉토리 ( ./)는 다음과 같은 내 홈 디렉토리입니다.
myhome
|- data (symlink to /mnt/data)
|- subdir (extra directory, for verbose explanation)
# os.path.abspath returns the absolute path, but does NOT resolve symlinks in its argument
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'
realpath()가 포함될 수 있으며..두 가지를 모두 사용하는 이유에 대한 질문에 실제로 대답하지 않는 것은 사실이 아닙니다 . jobrad의 답변이 더 정확합니다.