Python路径拼接

在Windows系统中,路径分隔符是反斜杠 \

而在Linux系统中,路径分隔符是正斜杠 /

为了在多平台上保持路径正确,应该使用os.path.join()函数来拼接路径,这样会根据当前系统的路径分隔符来自动调整。

BASE_DIRS = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

# image_path = fr"{BASE_DIRS}/static/base/diploma.png"


image_path = os.path.join(BASE_DIRS, 'static', 'base', 'diploma.png')

print('当前文件父级目录,一个是一层 os.path.dirname()', os.path.dirname(os.path.abspath(__file__)))
print('当前文件绝对路径', os.path.abspath(__file__))

你可能感兴趣的:(python)