pathlib.Path(__file__).parent

Published by onesixx on

pathlib.Path(__file__).parent와 pathlib.Path(__file__).parent.resolve()의 차이는
심볼릭 링크와 상대 경로를 어떻게 처리하는지에 있습니다.

pathlib.Path(__file__).parent

현재 파일의 부모 디렉토리를 제공합니다.
경로에 심볼릭 링크가 포함되어 있다면 그것들이 유지됩니다.
또한, 경로가 상대 경로라면 그대로 유지됩니다.

www_dir = pathlib.Path(file).parent / “www”

pathlib.Path(__file__).parent.resolve()

현재 파일의 부모 디렉토리에 대한 절대 경로를 제공하며, 이 과정에서 심볼릭 링크를 해결합니다.
즉, 경로에 심볼릭 링크가 있으면 그것들이 가리키는 실제 디렉토리로 대체됩니다.
또한, 경로가 상대 경로라면 resolve()는 그것을 절대 경로로 변환합니다

www_dir = BASE_PATH.parent.joinpath(‘data’).resolve()

from pathlib import Path

# __file__이 "/home/user/project/link_to_dir/file.py"라고 가정합시다.
# 여기서 "link_to_dir"는 "/home/user/dir"로의 심볼릭 링크입니다.

print(Path(__file__).parent)
# 출력: "/home/user/project/link_to_dir"

print(Path(__file__).parent.resolve())
# 출력: "/home/user/dir"

Path(__file__).parent는 심볼릭 링크 “link_to_dir”를 유지하는 반면, 
Path(__file__).parent.resolve()는 그것을 실제로 가리키는 디렉토리인 “dir”로 대체합니다.

Categories: Uncategorized

onesixx

Blog Owner

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x