如何获取其他文件夹的父文件夹?
问题描述:
在我的设置文件,我有以下行:如何获取其他文件夹的父文件夹?
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__))
是file_path
D:\Projects\Test\src\test
我会有D:\Projects\Test\src\webapp.cfg
。这在Ubuntu 8.04下不起作用。
EDIT1:我在虚拟机上运行Ubuntu 8.04。我已经安装了Python 2.5.2版本。
答
添加file_path = os.path.normpath(file_path)
以消除上一级参考(/../)。
答
parent = os.path.normpath(os.path.join(directory, ".."))
“不起作用”是什么意思?你的电脑爆炸了吗? `file_path`中有“粉色独角兽”吗?这个对我有用。 – SilentGhost 2011-02-02 15:07:15