在我的设置文件中有以下几行:
import os
import sys
PROJECT_ROOT = os.path.dirname(__file__)
sys.path.insert(0, os.path.join(PROJECT_ROOT, "apps"))
file_path = os.path.join(os.path.realpath(os.path.dirname(__file__)),"..", "webapp.cfg")
在 Windows 下,如果 os.path.realpath(os.path.dirname(__file__))
是 D:\Projects\Test\src\test
in file_path
我将拥有 D:\Projects\Test\src\webapp.cfg
。这在 Ubuntu 8.04 下不起作用。
编辑 1: 我在虚拟机上运行 Ubuntu 8.04。我在上面安装了 Python 2.5.2 版本。
最佳答案
添加 file_path = os.path.normpath(file_path)
以消除上层引用 (/../)。
关于python - 如何获取另一个文件夹的父文件夹?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4875937/