import os
# 獲取當前目錄
print(os.getcwd())
print(os.path.abspath(os.path.dirname(__file__)))
print(os.path.abspath('.'))
# 獲取當前文件
print(os.path.abspath(__file__))
# 獲取上級目錄
print(os.path.abspath(os.path.dirname(os.getcwd())))
print(os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
print(os.path.abspath(os.path.join(os.getcwd(), "..")))
# 獲取上上級目錄
print(os.path.abspath(os.path.join(os.getcwd(), "../..")))
註釋
os.getcwd() # Return a unicode string representing the current working directory
os.path.dirname() # Returns the directory component of a pathname
os.path.abspath() # Return the absolute version of a path