os.path.abspath와 os.path.realpath를 모두 사용하는 이유는 무엇입니까?
여러 오픈 소스 프로젝트에서 사람들이 하는 것을 보았습니다.os.path.abspath(os.path.realpath(__file__))
현재 파일에 대한 절대 경로를 가져옵니다.
하지만, 나는 그것을 발견합니다.os.path.abspath(__file__)
그리고.os.path.realpath(__file__)
같은 결과를 낳습니다.os.path.abspath(os.path.realpath(__file__))
약간 중복되는 것 같습니다.
사람들이 그것을 사용하는 이유가 있습니까?
당신의 언급된 시나리오에서, 실제 통화가 있기 때문에, 실제 경로와 abspath를 결합할 이유가 없습니다.os.path.abspath
결과를 반환하기 전에 (Python 2.5에서 Python 3.6으로 확인).
os.path.abspath
절대 경로를 반환하지만 인수의 심볼 링크는 확인하지 않습니다.os.path.realpath
먼저 경로의 심볼 링크를 확인한 다음 절대 경로를 반환합니다.
그러나 경로에 다음이 포함될 것으로 예상되는 경우~
abspath 또는 realpath는 사용자의 홈 디렉토리로 확인되지 않으며 결과 경로는 유효하지 않습니다.를 사용하여 이 문제를 사용자의 디렉토리로 해결해야 합니다.
자세한 설명을 위해 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'
os.path.realpath
이를 지원하는 운영 체제의 심볼릭 링크를 보호합니다.
os.path.abspath
같은 것들을 간단히 제거합니다..
그리고...
디렉터리 트리의 루트에서 명명된 파일(또는 symlink)까지의 전체 경로를 제공하는 경로에서
예를 들어 Ubuntu의 경우
$ ls -l
total 0
-rw-rw-r-- 1 guest guest 0 Jun 16 08:36 a
lrwxrwxrwx 1 guest guest 1 Jun 16 08:36 b -> a
$ python
Python 2.7.11 (default, Dec 15 2015, 16:46:19)
[GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from os.path import abspath, realpath
>>> abspath('b')
'/home/guest/play/paths/b'
>>> realpath('b')
'/home/guest/play/paths/a'
심볼릭 링크에는 상대 경로가 포함될 수 있으므로 둘 다 사용해야 합니다.에 대한 내부 전화:realpath
내장된 경로를 반환할 수 있음..
부품, 그것abspath
제거합니다.
레이맨 용어로, 바로 가기 파일의 경로를 가져오려는 경우 절대 경로는 바로 가기 위치에 있는 파일의 전체 경로를 제공하는 반면 실제 경로는 파일의 원래 위치 경로를 제공합니다.
절대 경로인 os.path.abspath()는 현재 작업 디렉터리 또는 사용자가 언급한 디렉터리에 있는 파일의 전체 경로를 제공합니다.
실제 경로인 os.path.realpath()는 참조 중인 파일의 전체 경로를 제공합니다.
예:
file = "shortcut_folder/filename"
os.path.abspath(file) = "C:/Desktop/shortcut_folder/filename"
os.path.realpath(file) = "D:/PyCharmProjects/Python1stClass/filename"
Python 3.8 기준으로 이미 제공된 답변 외에도os.path.realpath
대문자 드라이브 문자를 반환하는 반면os.path.abspath
드라이브 소문자를 반환합니다.
언급URL : https://stackoverflow.com/questions/37863476/why-would-one-use-both-os-path-abspath-and-os-path-realpath
'itsource' 카테고리의 다른 글
Angular 2에서 IE11 캐싱 GET 호출 방지 (0) | 2023.08.20 |
---|---|
base_url() 함수가 코드 점화기에서 작동하지 않습니다. (0) | 2023.08.20 |
파이썬 요청에서 쿠키를 사용하려면 어떻게 해야 합니까? (0) | 2023.08.20 |
git clone에 ssh 옵션 전달 중 (0) | 2023.08.20 |
디지털 오션에서 헤로쿠로 마이그레이션 (0) | 2023.08.20 |