Python 3.4 이상에서는 pathlib 모듈을 사용하여 상위 디렉토리를 가져올 수 있습니다. 예를 들어,
from pathlib import Path
print(Path('/home/username').parent)
This will give the output:
/home 이전 버전에서는 경로 및 '..'(상위 디렉토리를 나타냄)에서 os.path.join을 호출한 다음 os.path.abspath를 사용하여 절대 경로를 찾을 수 있습니다.
예를 들어
import os
print(os.path.abspath(os.path.join('/home/username', '..')))
This will give the output:
/home