test.py
import os print('***获取当前目录***') print(os.getcwd()) print(os.path.abspath(os.path.dirname(__file__))) print('***获取上级目录***') print(os.path.abspath(os.path.dirname(os.path.dirname(__file__)))) print(os.path.abspath(os.path.dirname(os.getcwd()))) print(os.path.abspath(os.path.join(os.getcwd(), ".."))) print('***获取上上级目录***') print(os.path.abspath(os.path.join(os.getcwd(), "../..")))
bogon:Desktop macname$ pwd /Users/macname/Desktop bogon:Desktop macname$ python3 test.py ***获取当前目录*** /Users/macname/Desktop /Users/macname/Desktop ***获取上级目录*** /Users/macname/Desktop /Users/macname /Users/macname ***获取上上级目录*** /Users