How can I obtain the parent folder of another folder?
In my settings file I have the following lines:
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")
Under Windows if os.path.realpath(os.path.dirname(__file__))
开发者_开发问答is D:\Projects\Test\src\test
in file_path
I will have D:\Projects\Test\src\webapp.cfg
. This doesn't work under Ubuntu 8.04.
EDIT1: I am running Ubuntu 8.04 on a virtual machine. I have Python 2.5.2 version installed on it.
Add file_path = os.path.normpath(file_path)
to eliminate the up-level reference (/../).
parent = os.path.normpath(os.path.join(directory, ".."))
精彩评论