用内置函数: __file__
给一个Django的示例,这是获取当前文件的上一级目录的实例。
import os# Build paths inside the project like this: os.path.join(BASE_DIR, ...)BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
import osos.getcwd()