开发者

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, ".."))
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜