ใน 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