2011-02-02 156 views
0

在我的設置文件,我有以下行:如何獲取其他文件夾的父文件夾?

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_pathD:\Projects\Test\src\test我會有D:\Projects\Test\src\webapp.cfg。這在Ubuntu 8.04下不起作用。

EDIT1:我在虛擬機上運行Ubuntu 8.04。我已經安裝了Python 2.5.2版本。

+1

「不起作用」是什麼意思?你的電腦爆炸了嗎? `file_path`中有「粉色獨角獸」嗎?這個對我有用。 – SilentGhost 2011-02-02 15:07:15

回答

3

添加file_path = os.path.normpath(file_path)以消除上一級參考(/../)。

0
parent = os.path.normpath(os.path.join(directory, "..")) 
相關問題